Paste a wave first Official Questions This contest has a greedy theme, a matrix fast power template, general difficulty, no knowledge point, tree-dependent Backpack
Unfortunately, the big simulation was not written out
A Coach's Confusion in Chuanchuan
Direct Violence Simulation
#include<bits/stdc++.h> using namespace std; vector<int> p; bool cmp(int a,int b) { return a>b; } int main() { int n,m; scanf("%d%d",&n,&m); p.resize(n); for(int i=0;i<n;i++) { scanf("%d",&p[i]); } sort(p.begin(),p.end(),cmp); int sum=p[0]+p[1]+p[2]; if(sum>m) printf("%d\n",sum); else printf("Waiver!"); return 0; }
B They say that the small town is expensive to cut cakes
First let's find out all the different letters that exist in a given string.We first iterate through the string to find all the different number of characters, maintain this number with map s and rulers, and then update the answer.
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; map<char, int> mp; int main() { int n; char s[maxn]; while(~scanf("%d", &n)){ mp.clear(); scanf("%s", s + 1); for(int i = 1; i <= n; i++) mp[s[i]]++; int num = mp.size(); int ans = n; int l = 1, r = 1; int now = num; mp.clear(); while(mp.size() != num) { mp[s[r]]++; r++; } r--; while(l <= n - num + 1 && r <= n) { if(now == num) { ans = min(ans, r - l + 1); mp[s[l]]--; if(mp[s[l]] == 0) now--; l++; } if(now < num) { r++; if(mp[s[r]] == 0) now++; mp[s[r]]++; } } printf("%d\n", ans); } }
The original theme, classic greed, interval greed need to be converted.See code for details
/* Firstly, the intervals where radar can be set up on the x-axis and projected to an island are determined. Then find the public part for each interval, and update if there is a public part The tail of the public section, if nonexistent, the number of points set up by the radar station+1 */ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<vector> using namespace std; const int MAXN=1011; struct node{ double l,r; }; vector<node> p; bool cmp(node a,node b) { if(a.l==b.l) return a.r<b.r; else return a.l<b.l; } int main(){ int n,cnt=1; double d; while(~scanf("%d%lf",&n,&d)) { if(n==0&&d==0) break; p.clear(); bool flag=true; for(int i=0;i<n;i++) { double x,y; scanf("%lf%lf",&x,&y); if(abs(y)>d) flag=false; else { node temp; temp.l=x-sqrt(d*d-y*y); temp.r=x+sqrt(d*d-y*y); p.push_back(temp); } } printf("Case %d: ",cnt++); if(!flag) { puts("-1"); continue; } else { sort(p.begin(),p.end(),cmp); int ans=1; double start=p[0].r; for(int i=1;i<n;i++) { if(start>=p[i].l) start=min(start,p[i].r); else { start=p[i].r; ans++; } } printf("%d\n",ans); } } return 0; }
simulation
#include<bits/stdc++.h> using namespace std; const int MAXN=1011; int mat[MAXN][MAXN]; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&mat[i][j]); } } for(int i=0;i<n;i++) { for(int j=n-1;j>=0;j--) { if(j==(n-1)) printf("%d",mat[j][i]); else printf(" %d",mat[j][i]); } printf("\n"); } return 0; }
Find the law, it's easier
#include<bits/stdc++.h> using namespace std; int main() { int n,m,k; scanf("%d%d%d",&n,&m,&k); int min1=min(n,m),maxn1=max(n,m); if(min1==1) { if(maxn1==1) { if(k>=1) puts("Beautiful flowers!"); else puts("Oh! My poor HJ!"); } else { if(maxn1==2) { if(k>=2) puts("Beautiful flowers!"); else puts("Oh! My poor HJ!"); } else { if(k>=2) puts("Beautiful flowers!"); else puts("Oh! My poor HJ!"); } } } else { if(min1==2) { if(k>=2) puts("Beautiful flowers!"); else puts("Oh! My poor HJ!"); } else { if(k>=2) puts("Beautiful flowers!"); else puts("Oh! My poor HJ!"); } } return 0; }
F Incompetent Fury of a Single Dog
simulation
#include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d",&n); set<char> p; string s; cin>>s; for(int i=0;i<s.length();) { if(!p.count(s[i])) { p.insert(s[i]); i++; } else s.erase(i,1); } cout<<s.length()<<endl; cout<<s; return 0; }
Simulating Dafa
#include<bits/stdc++.h> using namespace std; map<int,string> p; vector<int> q; void init() { p[1]="do",p[2]="re",p[3]="mi",p[4]="fa",p[5]="sol",p[6]="la",p[7]="si"; } int main() { init(); int n; scanf("%d",&n); q.resize(n); for(int i=0;i<n;i++) { scanf("%d",&q[i]); } int cnt=0; for(int i=0;i<n;i++) { string s; cin>>s; if(s==p[q[i]]) continue; else cnt++; } printf("%d\n",cnt); return 0; }
Purple book example, direct violence
#include<bits/stdc++.h> using namespace std; int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); switch(n) { case 0:{ cout<<"0"<<endl; break; } case 1:{ cout<<"1"<<endl; break; } case 2:{ cout<<"0"<<endl; break; } case 3:{ cout<<"0"<<endl; break; } case 4:{ cout<<"0"<<endl; break; } case 5:{ cout<<"1"<<endl; break; } case 6:{ cout<<"5"<<endl; break; } case 7:{ cout<<"33"<<endl; break; } case 8:{ cout<<"245"<<endl; break; } case 9:{ cout<<"2053"<<endl; break; } case 10:{ cout<<"19137"<<endl; break; } case 11:{ cout<<"196705"<<endl; break; } } } return 0; }
Matrix Quick Power Bare Question
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> using namespace std; const int M = 1e9+7; typedef long long ll; const int mod = 1e9+7; int n ; struct mat { ll a[15][15]; mat(){ //heavy load memset(a,0,sizeof(a)); } }; mat mul(mat x,mat y) { mat res; for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) res.a[i][j] = (res.a[i][j]+x.a[i][k]*y.a[k][j])%mod; return res; } ll qpow(int p) { mat bas;//Base Matrix mat res;//Unit Matrix for(int i=0;i<2;i++) res.a[i][i] = 1; bas.a[0][0] = bas.a[0][1] = bas.a[1][0] = 1; bas.a[1][1] = 0; while(p) { if(1&p) res = mul(res,bas); bas = mul(bas,bas); p>>=1; } return res.a[0][1];//Or res.a[1][0] } int main() { while(cin>>n) { cout<<qpow(n)<<endl; //f[n] = qpow(n) } return 0; }
According to DP, each line prefix and each column prefix are summed, and then hang up. Unfortunately.Because this matrix is a square matrix, not written in the same way as finding the maximum sum, the positive solution should be as follows: Because the answer is obviously monotonic, it is easy to see that it is a dichotomous topic. First, the two-dimensional prefix sum is preprocessed, and the complexity is O(n^2), then the dichotomous answer, because the two-dimensional prefix sum is preprocessed, the complexity of the current answer is O(n^2) every check., with an overall complexity of O(n^2*logn), you can pass the question.
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e3 + 5; long long a[MaxN][MaxN],k; int n; void inti(){ for(int i = 1 ; i <= n ; ++i){ for(int j = 1; j <= n ; ++j){ a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } } bool check(int x){ long long res; for(int i = x ; i <= n ; ++i){ for(int j = x ; j <= n ; ++j){ res = a[i][j] - a[i - x][j] - a[i][j - x] + a[i - x][j - x]; if(res >= k) return true; } } return false; } int main(){ scanf("%d %lld",&n,&k); for(int i = 1; i <= n ; ++i) for(int j = 1;j <= n ; ++j) scanf("%lld",&a[i][j]); inti(); int l = 1,r = n,ans = 0; while(l <= r){ int mid = l + r >> 1; if(check(mid)){ ans = mid ; r = mid - 1; } else l = mid + 1; } if(!ans) puts("I'm a Gold Chef!"); else printf("%d\n",ans); return 0; }