B、减成一
简单dp,差分处理,你会发现如果一个数比前一个数小,那么这个数可以在处理前面数的时候处理完毕。
一个数比前面数大,那么多花的步骤就是和前面一个数的差值。左边取在这个点,右边可以随便控制。
#include <iostream> #include <algorithm> using namespace std; const int MAX = 1e5 + 7; int info[MAX], dp[MAX]; int main() { int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> info[i]; dp[0] = 0; info[0] = 1; for (int i = 1; i <= n; i++) { if (info[i] > info[i - 1]) dp[i] = dp[i - 1] + info[i] - info[i - 1]; else dp[i] = dp[i - 1]; } cout << dp[n] << endl; } return 0; }
C、面积
签到题,不会吧不会吧签到题还有人WA?
#include<bits/stdc++.h> using namespace std; int main() { double t, x, sum; cin >> t; while (t--) { cin >> x; sum = x * x + 2 * 3.14 * (x / 2) * (x / 2); printf("%.2f\n", sum); } }
E、赛马
第一眼以为是贪心,打算对2个人都排序然后用游标计数……再仔细看个题,发现B数组不能动。
那就更简单了,直接把A数组排个序,再去二分找顺序输入的B元素第一个大于的值。以为你留着赢下一把也是+1,还不如现在就比了。
所以直接STL找上界即可。
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) const int mod = 1e9+7; const int maxn = 10005; #define stop system("pause") inline ll read(){ ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } multiset<int> q; int main(){ int T = read(); while(T--){ q.clear(); int n = read(); for(int i = 0 ; i < n ;++i){ int tmp = read(); q.insert(tmp); } int ans = 0; for(int i = 0 ; i < n ;++i){ int tmp = read(); auto it = upper_bound(q.begin(),q.end(),tmp); if(it != q.end()){ ++ans; q.erase(it); } } cout<<ans<<endl; } }
F、三角形
很容易发现按斐波那契数列分隔即可最多。那么问题就变成了,最多可以分成几个斐波那契前n项和。题目居然没出ull……ll的输入居然A了。。搞得我用了个__int128。怕加法溢出,最后会发现只要100多一点点就可以超过ull的范围了。
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) const int mod = 1e9 + 7; const int maxn = 100005; inline ull read() { ull s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } __int128 fib[10005] = { 0,1,1 }; int main() { for (int i = 3; i < 1005; ++i) fib[i] = fib[i - 1] + fib[i - 2]; // for(int i = 3 ; i < 1005 ; ++i) cout<<fib[i]<<endl; int t = read(); while (t--) { ull a = read(); if (a < 3) { cout << a << endl; continue; } ll ans; for (int i = 3; i < 1005; ++i) { if (fib[i] - 1 > a) { ans = i - 3; break; } } cout << ans << endl; } return 0; }
H、直线
一看就是大数,要么套要么你就打大数的板子……建议学下py
t=int(input()) while t>0: n=int(input()) print((n*(n-1)//2)) t-=1
J、最大值
KMP的模板题,只要你能发现待匹配串是除开第一个字符后面的串,去匹配的是输入字符串s。
j在的位置就是长度。
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) const int maxn = 100005; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } int cnt = 0, ans = 0; int Next[maxn]; void getnext(char b[]) { int i, j, len = strlen(b); Next[0] = -1; for (i = 0, j = -1; i < len;) if (j == -1 || b[i] == b[j]) { ++i; ++j; if (b[i] != b[j]) Next[i] = j; else Next[i] = Next[j]; } else j = Next[j]; } void kmp(char a[], char b[]) { int i, j, n, len; getnext(b); n = strlen(a); len = strlen(b); // cout<<" n len "<<n<<" "<<len<<endl; for (i = j = 0; i < n && j < len;) { if (j == -1 || a[i] == b[j]) { i++, j++; // cout<<"ahdioh "<<a[i] << " dasi "<<b[j]<<endl; } else j = Next[j], cnt = 0; ans = max(j, ans); } } char a[maxn], b[maxn]; int main() { js; int n; cin >> n; while (n--) { ans = 0; cin >> b; strcpy(a, b + 1); kmp(a, b); cout << ans << endl; } return 0; }