codeforce790div4-EFG
F. Longest Strike
思路:双指针统计区间个数,注意越界情况
#pragma GCC optimize(2) #pragma GCC optimize(1) #include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; const ull base=131; #define MAX 200009 #define PI 3.141592653589793 using namespace std; int mm[MAX]; inline void solve(){ int len,k; cin>>len>>k; map<int,int> check; for(int i=1;i<=len;i++){ cin>>mm[i];check[mm[i]]++; } vector<int> op; for(auto x:check){ if((x.second)>=k) op.push_back((x.first)); } if(op.size()==0){ cout<<-1<<"\n";return ; } sort(op.begin(),op.end()); int olen=op.size(),l=0,r=1,men=0,tl=op[0],tr=op[0],tlen=1; for(;r<olen;r++){ if(op[r]==op[r-1]+1){ tlen++; if(tlen>men){ men=tlen; tl=op[l];tr=op[r]; } } else{ tlen=1;l=r; } } cout<<tl<<' '<<tr<<endl; } int main(){ int sum;cin>>sum; while(sum--){ solve(); } }
E
思路:二分加贪心
#pragma GCC optimize(2) #pragma GCC optimize(1) #include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; const ull base=131; #define MAX 150009 #define PI 3.141592653589793 using namespace std; int meme[MAX],pre[MAX]; bool cmp(int a,int b){ return a>b; } inline void solve(){ int len,k;cin>>len>>k; for(int i=1;i<=len;i++) cin>>meme[i]; sort(meme+1,meme+len+1,cmp); for(int i=1;i<=len;i++) pre[i]=pre[i-1]+meme[i]; for(int i=0;i<k;i++){ int temp;cin>>temp; int check=lower_bound(pre+1,pre+1+len,temp)-pre; if(check==len+1) cout<<-1<<"\n"; else cout<<check<<"\n"; } } int main(){ int sum;cin>>sum; while(sum--){ solve(); } }
G
思路:有向图建树,利用dfs回溯统计树的平衡状态
#pragma GCC optimize(2) #pragma GCC optimize(1) #include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; const ull base=131; #define MAX 150009 #define PI 3.141592653589793 using namespace std; int ans; vector<int> edge[MAX]; char color[MAX]; int dfs(int fa,int now){ int value;if(color[now]=='B') value=-1;else value=1; if(fa==now) return 0; for(auto y:edge[now]){ value+=dfs(now,y); } if(value==0) ans++; return value; } inline void solve(){ int fa; int len;cin>>len; for(int i=2;i<=len;i++){ int temp;scanf("%d",&temp); edge[temp].push_back(i); if(i==temp) fa=i; } getchar(); scanf("%s",color+1); ans=0; dfs(0,1); cout<<ans<<endl; for(int i=1;i<=len;i++) edge[i].clear(); } int main() { int sum;scanf("%d",&sum); while(sum--){ solve(); } }