This question will be of a [i] array according to a direct operation, may be exchanged with the foregoing, the position may change.
Then you want to find the minimum and maximum for each position
Said former title do you want to analyze, but I saw a few times still can not come up with anything simple algorithm
But in fact, I will not analyze
Because the simulation is kind of practice, and the meaning of problems by simulation, then, is nothing but a traversal of the array, only the complexity of O (m) only. This is the wish complicated, you should consider adding simulation, and complexity can not be judged too
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int maxn=1e5+7; int n,m,a[maxn*4],l[maxn],h[maxn],num[maxn],pos[maxn]; ///数字i的位置是pos[i],位置i的数字是num[i] int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) l[i]=h[i]=num[i]=pos[i]=i; for(int i=1;i<=m;i++){ int psi=pos[a[i]]; if(psi==1)continue; int t=num[psi-1];///t是前一个位置的数字 pos[a[i]]=psi-1; pos[t]=psi; num[psi]=t; num[psi-1]=a[i]; l[a[i]]=min(l[a[i]],psi-1); h[t]=max(h[t],psi); } for(int i=1;i<=n;i++) cout<<l[i]<<' '<<h[i]<<endl; return 0; }
This question is obviously a half ah
Determine the size of the room, and then try to place a single or a house c
If you have a house at the same time finally put a and c, then fail
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; int ce,a,b,c; bool check(int mid){ if(a>mid&&c>mid)return false; if(a<=mid&&c<=mid)return true; if(a>mid) return a<=mid*2; if(c>mid) return c<=mid*2; } int main(){ scanf("%d",&ce); while(ce--){ scanf("%d%d%d",&a,&b,&c); int l=(a+b+c-1)/3+1,r=max(a,max(b,c)),ans; while(l<=r){ int mid=(l+r)>>1; if(check(mid)){ ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); } return 0; }
I summed up a common mistake - is my favorite ++, -, + =, - =, and then will be judged in a number of problems in WA
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> using namespace std; const int maxk=8007; const int inf=0x7fffffff; int n,m,k,pep[maxk],maxroom; int main(){ scanf("%d%d",&n,&k); for(int i=0;i<n;i++){ scanf("%d",&m); pep[m]++; } sort(pep+1,pep+1+k); if(k==1)maxroom=pep[1]; else{ for(int i=1,j=k;j-i>=1;i++,j--) maxroom=max(maxroom,pep[i]+pep[j]); } int ans=inf; for(int i=pep[k];i<=maxroom;i++){ int l=1,h=k,r=0; while(l<=h){ if(pep[l]+pep[h]<=i) l++,h--; else h--; r++; } ans=min(ans,i*r); } printf("%d\n",ans); return 0; }
The number 1 is given to the number N of tall people, like arranged in k rows, each row of the highest minimum gap height is not greater than 1, each row of the same number of people, ask people queuing up to participate in the
I just did it on the road and the road is half before we thought it was the topic, and then divided to two questions on the
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; typedef long long LL; const int maxn=3e4+7; int ce,n; LL k,c[maxn],sum,l,r,ans; bool check(LL mid){ LL numk=0,lef=0; for(int i=1;i<=n;i++){ LL t=(lef+c[i])/mid; numk+=t; if(numk>=k)return true; lef=lef>=t*mid?c[i]:c[i]+lef-t*mid; } return false; } int main(){ scanf("%d",&ce); while(ce--){ scanf("%d%lld",&n,&k); sum=0; for(int i=1;i<=n;i++){ scanf("%lld",&c[i]); sum+=c[i]; } l=1,r=sum/k,ans=-1; while(l<=r){ LL mid=(l+r)>>1; if(check(mid)){ ans=mid; l=mid+1; } else r=mid-1; } printf("%lld\n",ans!=-1?ans*k:0); } return 0; }
I think it is very complex road graph theory. While others see the explanation is simple graph theory problem, but the code is still very honest ...
To know the make the job chart
This segment tree did not even read the first mark about it