19.11.17ACM training Replenishment

CodeForces - 1250A

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;
}
View Code

 

CodeForces - 1250L

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;
}
View Code

I summed up a common mistake - is my favorite ++, -, + =, - =, and then will be judged in a number of problems in WA

 

 
This question is the meaning of the questions selected cabin size s, s guarantee each team at least can hold a maximum of two teams loaded and shipped shipped complete r times
The resulting minimum cost s * r

In front of me did a half, and then look at this question, like half
This question is obviously not a condition but judging success, but how much to spend
So I decisively chose the three-point answer, let's spend a little LR approximation answer, then WA
Why WA it, because even though I know it takes s calculated by need, but did not take into account the cost to relations with monotonous s
For example with the cabin 3 3 2 3 3 9 takes transported, the transport carriage 4 takes the two transport carriages takes three times with 10 12,5
So one-third is not enough (although it had more than 20 samples)
Then watched solution to a problem, this question should be greedy
Greedy strategy is to try to put more than the number of teams and the small number of teams put together, so that each compartment were almost, and then particularly large number of teams put a separate compartment. Ideally number of how many people spend
#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;
}
View Code

 

CodeForces - 1250J

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

 

Under the problem in a binary, determine a row number, row can then read this row, and then obtain k discharge condition, a row number of the most
#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;
}
View Code

 

CodeForces - 1250N

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

answer

 

CodeForces - 1250C

This segment tree did not even read the first mark about it

answer

Guess you like

Origin www.cnblogs.com/helman/p/11893989.html