此合集收录了各种有趣的题解
K-th Not Divisible by n https://codeforces.com/contest/1352/problem/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std;int main () { int t; cin >> t; while (t--){ int k, n; cin >> n >> k; int need = (k - 1 ) / (n - 1 ); cout << k + need << endl; } return 0 ; }
Raising Bacteria https://codeforces.com/problemset/problem/579/A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std;int main () { int n; cin >> n; int count = 0 ; while (n > 0 ) { int r = n % 2 ; n /= 2 ; if (r == 1 ) count++; } cout << count << endl; return 0 ; }
Same Differences https://codeforces.com/contest/1520/problem/D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <iostream> #include <map> #define int long long using namespace std;signed main () { int t; cin >> t; while (t--){ map<int , int > cnt; int ans= 0 ; int n; cin >> n; for (int i = 0 ; i < n; i++) { int x; cin >> x; x -= i; ans += cnt[x]; cnt[x]++; } cout << ans << endl; } return 0 ; }
最小循环节 https://ac.nowcoder.com/acm/contest/97443/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ); string str; cin >> str; map<char , int > cnt; for (auto t : str){ cnt[t]++; } cout << cnt.size () << endl; return 0 ; }
Move Brackets https://codeforces.com/problemset/problem/1374/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <bits/stdc++.h> using namespace std;#define int long long void solve () { int n; cin >> n; string str; cin >> str; int ans = 0 ; int bal = 0 ; for (int i = 0 ; i < n; i++) { if (str[i] == '(' ) bal++; else { bal--; if (bal < 0 ) { bal = 0 ; ans++; } } } cout << ans << endl; } signed main () { ios::sync_with_stdio (false ); cin.tie (nullptr ); int t; cin >> t; while (t--){ solve (); } return 0 ; }
Sorted Adjacent Differences https://codeforces.com/problemset/problem/1339/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; int arr[n]; int ans[n]; for (int i = 0 ; i < n; i++) { cin >> arr[i]; } sort (arr, arr + n); int l, r; if (n % 2 == 0 ) { l = n / 2 - 1 ; r = n / 2 ; } else { l = n / 2 ; r = n / 2 + 1 ; } int k = 0 ; while (1 ) { if (l >= 0 ) { ans[k++] = arr[l--]; } if (r <= n - 1 ) { ans[k++] = arr[r++]; } if (k == n) { break ; } } for (int i = 0 ; i < n; i++) { cout << ans[i] << " " ; } cout << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--) { solve (); } return 0 ; }
pspspsps https://codeforces.com/contest/2049/problem/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; string str; cin >> str; if (str[0 ] == 's' ) str[0 ] = '.' ; if (str[n - 1 ] == 'p' ) str[n - 1 ] = '.' ; bool found_p = false ; bool found_s = false ; for (int i = 0 ; i < n; i++) { if (str[i] == 'p' ) { found_p = true ; } else if (str[i] == 's' ) { found_s = true ; } } if (found_p && found_s) { cout << "NO" << endl; } else { cout << "YES" << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--) { solve (); } return 0 ; }
小红的排列构造 https://ac.nowcoder.com/acm/contest/99277/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; int arr[n + 1 ]; string str; cin >> str; str = ' ' + str; if (str[n] == '0' ) { cout << -1 << endl; return ; } int flag = 1 ; for (int i = 1 ; i <= n; i++) { if (str[i] == '1' ) { arr[i] = flag; flag = i + 1 ; } else { arr[i] = i + 1 ; } } for (int i = 1 ; i <= n; i++) { cout << arr[i] << " " ; } cout << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--) { solve (); } return 0 ; }
小红的01串(三) https://ac.nowcoder.com/acm/contest/98256/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 #include <bits/stdc++.h> using namespace std;#define int long long void solve () { int a, b, k; cin >> a >> b >> k; int max_k; if (k == 0 && a != 0 && b != 0 ) { cout << -1 << endl; return ; } if (a == b) { max_k = 2 * a - 1 ; } else { max_k = 2 * min (a, b); } if (k <= max_k) { if (a > b) { if (k % 2 != 0 ) { int r0 = a - ((k + 1 ) / 2 ); int r1 = b - ((k + 1 ) / 2 ); for (int i = 0 ; i < r0; i++) { cout << "0" ; } for (int i = 0 ; i < ((k + 1 ) / 2 ); i++) { cout << "01" ; } for (int i = 0 ; i < r1; i++) { cout << "1" ; } } else { k--; int r0 = a - ((k + 1 ) / 2 ) - 1 ; int r1 = b - ((k + 1 ) / 2 ); for (int i = 0 ; i < r0; i++) { cout << "0" ; } for (int i = 0 ; i < ((k + 1 ) / 2 ); i++) { cout << "01" ; } for (int i = 0 ; i < r1; i++) { cout << "1" ; } cout << "0" ; } cout << endl; } else { if (k % 2 != 0 ) { int r0 = a - ((k + 1 ) / 2 ); int r1 = b - ((k + 1 ) / 2 ); for (int i = 0 ; i < r1; i++) { cout << "1" ; } for (int i = 0 ; i < ((k + 1 ) / 2 ); i++) { cout << "10" ; } for (int i = 0 ; i < r0; i++) { cout << "0" ; } } else { k--; int r0 = a - ((k + 1 ) / 2 ); int r1 = b - ((k + 1 ) / 2 ) - 1 ; for (int i = 0 ; i < r1; i++) { cout << "1" ; } for (int i = 0 ; i < ((k + 1 ) / 2 ); i++) { cout << "10" ; } for (int i = 0 ; i < r0; i++) { cout << "0" ; } cout << "1" ; } cout << endl; } } else { cout << -1 << endl; } } signed main () { ios::sync_with_stdio (false ); cin.tie (nullptr ); int t; cin >> t; while (t--) { solve (); } return 0 ; }
预知 https://ac.nowcoder.com/acm/contest/99458/D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; int arr[n]; int cnt_1 = 0 ; for (int i = 0 ; i < n; i++) { cin >> arr[i]; if (arr[i] == 1 ) cnt_1++; } if (n == 1 ) { cout << -1 << endl; return ; } sort (arr, arr + n); if (cnt_1 == n) { cout << 0 << endl; return ; } else if (arr[n - 2 ] == 1 ) { cout << arr[n - 1 ] - 1 << endl; return ; } cout << arr[n - 1 ] << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--){ solve (); } return 0 ; }
Petr and a Combination Lock https://codeforces.com/problemset/problem/1097/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; vector<int > arr (n) ; for (int i = 0 ; i < n; i++) cin >> arr[i]; int sum = 1 << n; for (int mask = 0 ; mask < sum; mask++) { int angle = 0 ; for (int i = 0 ; i < n; i++) { if (mask & (1 << i)) { angle -= arr[i]; } else { angle += arr[i]; } } if (angle % 360 == 0 ) { cout << "YES" << endl; return ; } } cout << "NO" << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--) { solve (); } return 0 ; }
Dora and Search https://codeforces.com/problemset/problem/1793/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; vector<int > a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; int l = 0 , r = n - 1 ; int MIN = 1 , MAX = n; while (l <= r) { if (a[l] == MIN) { l++; MIN++; } else if (a[l] == MAX) { l++; MAX--; } else if (a[r] == MIN) { r--; MIN++; } else if (a[r] == MAX) { r--; MAX--; } else { break ; } } if (l <= r) { cout << l + 1 << " " << r + 1 << endl; } else { cout << -1 << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--){ solve (); } return 0 ; }
小红的双生排列 https://ac.nowcoder.com/acm/contest/99784/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e9 + 7 ;int factorial (int n) { int result = 1 ; for (int i = 1 ; i <= n; ++i) { result = (result * i) % N; } return result; } void solve () { int n; cin >> n; if (n % 2 == 0 ) { cout << 2 * factorial (n / 2 ) * factorial (n / 2 ) % N << endl; } else { cout << factorial (n / 2 ) * factorial (n / 2 + 1 ) % N << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
https://codeforces.com/problemset/problem/520/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n, m; cin >> n >> m; int ans = 0 ; if (m <= n) { ans = n - m; } else { while (1 ) { if (m % 2 == 0 && m >= n) { ans++; m /= 2 ; } else { ans++; m++; } if (m == n) break ; } } cout << ans << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
Sort the Array https://codeforces.com/problemset/problem/451/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; vector<int > arr (n + 2 ) ; arr[0 ] = INT_MIN; arr[n + 1 ] = INT_MAX; vector<pair<int , int >> p; for (int i = 1 ; i <= n; i++) cin >> arr[i]; if (n == 1 ) { cout << "yes" << endl; cout << 1 << " " << 1 << endl; return ; } else if (n == 2 ) { cout << "yes" << endl; if (arr[1 ] > arr[2 ]) { cout << 1 << " " << 2 << endl; } else { cout << 1 << " " << 1 << endl; } return ; } for (int i = 1 ; i < n; i++) { if (arr[i] > arr[i + 1 ]) { int l = i; int j = i; while (j < n && arr[j] > arr[j + 1 ]) j++; int r = j; p.push_back ({l, r}); i = j; } } if (p.size () == 1 ) { if (arr[p[0 ].second] > arr[p[0 ].first - 1 ] && arr[p[0 ].first] < arr[p[0 ].second + 1 ]) { cout << "yes" << endl; cout << p[0 ].first << " " << p[0 ].second << endl; } else { cout << "no" << endl; } } else if (p.size () == 0 ) { cout << "yes" << endl; cout << 1 << " " << 1 << endl; } else { cout << "no" << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
Even-Odd Game https://codeforces.com/problemset/problem/1472/D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; int ans = 0 ; vector<int > a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; sort (a.rbegin (), a.rend ()); for (int i = 0 ; i < n; i++) { if (i % 2 == 0 ) { if (a[i] % 2 == 0 ) { ans += a[i]; } } else { if (a[i] % 2 == 1 ) { ans -= a[i]; } } } if (ans == 0 ) { cout << "Tie" << endl; } else if (ans > 0 ) { cout << "Alice" << endl; } else { cout << "Bob" << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--) { solve (); } return 0 ; }
Mere Array https://codeforces.com/problemset/problem/1401/C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; vector<int > arr (n) ; for (int i = 0 ; i < n; i++) { cin >> arr[i]; } int min_num = *min_element (arr.begin (), arr.end ()); vector<int > sorted_arr = arr; sort (sorted_arr.begin (), sorted_arr.end ()); int flag = 0 ; for (int i = 0 ; i < n; i++) { if (arr[i] != sorted_arr[i] && arr[i] % min_num != 0 ) { flag = 1 ; break ; } } if (flag) { cout << "NO" << endl; } else { cout << "YES" << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--){ solve (); } return 0 ; }
最大子段和 https://www.luogu.com.cn/problem/P1115
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; vector<int > arr (n + 1 ) ; vector<int > sum (n + 1 ) ; int ans = INT_MIN; for (int i = 1 ; i <= n; i++) { cin >> arr[i]; if (i == 1 ) sum[i] = arr[i]; else sum[i] = max (arr[i], sum[i - 1 ] + arr[i]); ans = max (ans, sum[i]); } cout << ans << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
K倍区间 https://www.luogu.com.cn/problem/P8649
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <bits/stdc++.h> #define int long long using namespace std;int C (int n) { return n * (n - 1 ) / 2 ; } void solve () { int n, k; cin >> n >> k; vector<int > arr (n + 1 ) ; vector<int > sum (n + 1 ) ; vector<int > mod (n + 1 ) ; int ans = 0 ; for (int i = 1 ; i <= n; i++) { cin >> arr[i]; sum[i] = sum[i - 1 ] + arr[i]; } for (int i = 0 ; i <= n; i++) { mod[sum[i] % k]++; } for (int i = 0 ; i < k; i++) { ans += C (mod[i]); } cout << ans << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
XXXXX https://codeforces.com/contest/1364/my
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n, x; cin >> n >> x; vector<int > arr (n) ; int sum = 0 , l = -1 , r; for (int i = 0 ; i < n; i++) { cin >> arr[i]; sum += arr[i]; if (arr[i] % x != 0 ) { if (l == -1 ) l = i; r = i; } } if (sum % x != 0 ) { cout << n << endl; } else if (l == -1 ) { cout << -1 << endl; } else { cout << max (r, n - l - 1 ) << endl; } } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; cin >> t; while (t--){ solve (); } return 0 ; }
https://www.luogu.com.cn/problem/P4552
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e5 + 9 ;int n;int arr[N];int diff[N];void solve () { cin >> n; int x = 0 , y = 0 ; for (int i = 1 ; i <= n; i++) { cin >> arr[i]; diff[i] = arr[i] - arr[i - 1 ]; if (i != 1 ) { if (diff[i] > 0 ) x += diff[i]; else if (diff[i] < 0 ) y -= diff[i]; } } cout << max (x, y) << endl; cout << abs (x - y) + 1 << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
积木大赛 https://www.luogu.com.cn/problem/P1969
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n; cin >> n; int h = 0 ; int ans = 0 ; for (int i = 1 ; i <= n; i++) { int t; cin >> t; if (t > h) { ans += (t - h); } h = t; } cout << ans << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--){ solve (); } return 0 ; }
Cost of the Array https://codeforces.com/contest/2059/problem/B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <bits/stdc++.h> #define int long long using namespace std;void solve () { int n, k; cin >> n >> k; k /= 2 ; vector<int > a (n) ; for (int i = 0 ; i < n; i++) { cin >> a[i]; } if (2 * k == n) { for (int i = 1 ; i < n; i += 2 ) { if (a[i] != (i + 1 ) / 2 ) { cout << (i + 1 ) / 2 << endl; return ; } } cout << k + 1 << endl; } else { for (int i = 1 ; i <= n - 2 * k + 1 ; i++) { if (a[i] != 1 ) { cout << 1 << endl; return ; } } cout << 2 << endl; } } signed main () { ios::sync_with_stdio (0 ); cin.tie (0 ); cout.tie (0 ); int t = 1 ; cin >> t; while (t--) { solve (); } return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 https: #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e5 + 10 ;int n, m;int ans[N];vector<int > g[N]; void dfs (int x, int d) { if (ans[x]) return ; ans[x] = d; for (int i = 0 ; i < g[x].size (); i++) { dfs (g[x][i], d); } } void solve () { cin >> n >> m; for (int i = 1 ; i <= m; i++) { int u, v; cin >> u >> v; g[v].push_back (u); } for (int i = n; i >= 1 ; i--) dfs (i, i); for (int i = 1 ; i <= n; i++) cout << ans[i] << " " ; cout << endl; } signed main () { ios::sync_with_stdio (0 );cin.tie (0 );cout.tie (0 ); int t = 1 ; while (t--) { solve (); } return 0 ; }