没读题,看样例后就直接写了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); int t; cin >> t; while (t--) { string s; cin >> s; cout << count (s.begin (), s.end (), '1' ) << endl; } 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 #include <bits/stdc++.h> using namespace std;int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); int t; cin >> t; while (t--) { int n; cin >> n; vector<int > a (n) ; bool ok = true ; for (int i = 0 ; i < n; ++i) { cin >> a[i]; if (a[i] <= 2 * max (i, n - i - 1 )) { ok = false ; } } cout << (ok ? "YES" : "NO" ) << endl; } 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 #include <bits/stdc++.h> using ll = long long ;using namespace std;int main () { ios::sync_with_stdio (false ); cin.tie (nullptr ), cout.tie (nullptr ); int t; cin >> t; while (t--) { int n; cin >> n; vector<ll> a (n) ; for (int i = 0 ; i < n; ++i) { cin >> a[i]; } ll maxx = accumulate (a.begin (), a.end (), 0LL ); for (int t = 1 ; t < n; t++) { vector<ll> b (n - t) ; for (int i = 0 ; i < n - t; i++) { b[i] = a[i + 1 ] - a[i]; } a = b; maxx = max (maxx, abs (accumulate (a.begin (), a.end (), 0LL ))); } cout << maxx << endl; } return 0 ; }