c2:Increasing Subsequence (hard version)
那边小取那边,然后相等比较后面的长度
#include<bits/stdc++.h> using namespace std; #define maxn 500005 int a[maxn]; int main(){ int n,mx=0,in; scanf("%d",&n); for(int j=0;j<n;j++){ scanf("%d",&a[j]); if(mx<a[j]){ mx = a[j]; in = j; } } string s=""; int l = 0,r = n-1,k=-1; while(l<=r){ if(k<min(a[l],a[r])){ if(a[l]<a[r]){ s+="L"; k=a[l]; l++; }else if(a[l]>a[r]){ s+='R'; k=a[r]; r--; }else{ int res1=0,x=k; int res2=0,y=k; for(int i = l;i<=in;i++){ if(a[i]>x){ x = a[i]; res1++; }else break; } for(int i = r;i>=in;i--){ if(a[i]>y){ y = a[i]; res2++; }else break; } if(res1>res2){ k = a[l]; l++; s+='L'; }else{ k = a[r]; r--; s+='R'; } } }else if(k<a[l]){ s+='L'; k = a[l]; l++; }else if(k<a[r]){ s+='R'; k = a[r]; r--; }else break; } cout<<s.size()<<endl; cout<<s<<endl; }
#include<bits/stdc++.h> using namespace std; #define maxn 500005 #define LL long long LL a[maxn]; int main(){ LL n,m; scanf("%lld%lld",&n,&m);for(int j=0;j<m;j++){ a[j] = (n - (m - j) * (m - 1 -j) / 2) / (m-j); //cout<<a[j]<<endl; if(j>0&&a[j]>a[j-1]*2){ a[j]=a[j-1]*2; } n-=a[j]; if(n<0){ cout<<"NO"<<endl; return 0; } } if(n){ cout<<"NO"<<endl; return 0; } cout<<"YES"<<endl; for(int j=0;j<m;j++){ cout<<a[j]<<" "; } }
#include<bits/stdc++.h> using namespace std; #define maxn 500005 #define LL long long int a[maxn]; map<int,int>mp; set<int>s; vector<int>Q; int main(){ int n; cin>>n; for(int j=0;j<n;j++){ cin>>a[j]; } for(int j=0;j<n;j++){ int x; cin>>x; mp[x]++; s.insert(x); } for(int j=0;j<n;j++){ int x = (n-a[j])%n; set<int> ::iterator it = s.lower_bound(x); if(it==s.end()){ it=s.begin(); } if(mp[*it]>0){ mp[*it]--; cout<<(*it+a[j])%n<<" "; } if(mp[*it]==0){ s.erase(it); } } }
#include<bits/stdc++.h> using namespace std; #define maxn 500005 #define LL long long int a[maxn]; map<int,int>mp; set<int>s; vector<int>q,w; int main(){ int n; cin>>n; for(int j=0;j<n;j++){ scanf("%d",&a[j]); mp[a[j]]++; q.push_back(a[j]); } sort(q.begin(),q.end()); q.erase(unique(q.begin(),q.end()),q.end()); int l =0,mx = 0,res = 0,r=-1; for(int j=0;j<q.size();j++){ if(res==0){ res+=mp[q[j]]; if(res>mx){ mx = res; r = j; } }else{ if(q[j]==q[j-1]+1){ if(mp[q[j]]>=2){ res+=mp[q[j]]; if(res>mx){ mx = res; r = j; } }else{ res+=mp[q[j]]; if(res>mx){ mx = res; r = j; } res = mp[q[j]]; } }else{ res=mp[q[j]]; if(res>mx){ mx = res; r = j; } } } } // if(mx==21){ // cout<<q[r]<<endl; // } // cout<<mx<<" "<<r<<endl; int s = mp[q[r]]; for(int j=r-1;j>=0;j--){ if(q[j]+1!=q[j+1]){ l=j+1; break; }else{ s+=mp[q[j]]; if(s==mx){ l=j; break; } } } // cout<<l<<endl; for(int j=l;j<=r;j++){ while(mp[q[j]]>1){ w.push_back(q[j]); mp[q[j]]--; } } for(int j=r;j>=l;j--){ w.push_back(q[j]); } cout<<w.size()<<endl; for(int j=0;j<w.size();j++){ cout<<w[j]<<" "; } }