基础算法训练1

A(bzoj1650)

#include<bits/stdc++.h>

using namespace std;
int a[200200];
    int l,n,m;
int check(int x) {
    int now = 0;
    int mi = 0;
    for(int i = 1;i <= n;i++) {
        if(a[i] - a[now] < x) {
            mi++;
        } else {
            now = i;
        }
    }
    return mi <= m;
}
int main(){

    scanf("%d%d%d",&l,&n,&m);
    for(int i =1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+1+n);
    n++;
    a[n] = l;
    int li = 1;
    int ri = l;
    while(li <= ri) {
        int mid = (li+ri)/2;
        if(check(mid)) {
            li = mid + 1;
        } else {
            ri = mid - 1;
        }
    }
    printf("%d\n",ri);
}
View Code

B(bzoj2014)

#include<bits/stdc++.h>

using namespace std;
struct node{
    long long p;
    long long num;
};
node no[100100];
int cmp(node x,node y) {
    return x.p < y.p;
}
int main(){
    int n;
    long long b;
    scanf("%d%lld",&n,&b);
    for(int i = 1;i <= n;i++) {
        scanf("%lld%lld",&no[i].p,&no[i].num);
    }
    long long ans = 0;
    sort(no+1,no+1+n,cmp);
    for(int i = 1;i <= n;i++) {
        if(b/no[i].p >= no[i].num) {
            b-=no[i].p*no[i].num;
            ans+=no[i].num;
        } else {
            ans += b/no[i].p;
            b%=no[i].p;
        }
    }
    printf("%lld\n",ans);
} 
View Code

C(bzoj1639)

#include<bits/stdc++.h>

using namespace std;
long long a[200200];
int n,m;
int check(long long now) {
    long long sum = 0; 
    int num = 0;
    for(int i = 1;i <= n;i++) {
        if(a[i] > now) {
            return 0;
        }
        if(sum + a[i] > now) {
            sum = 0;
            num++;
            sum = a[i];
        } else {
            sum += a[i];
        }
    }
    num++;
    return num <= m;
}
int main(){
    scanf("%d%d",&n,&m);
    long long l,r = 0;
    for(int i = 1;i <= n;i++) {
        scanf("%lld",&a[i]);
        r+=a[i];
    }
    
    while(l <= r) {
        long long mid = (l+r)/2;
        if(check(mid)) {
            r = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    printf("%lld\n",l);
    
}
View Code

D(bzoj2016)

#include<bits/stdc++.h>

using namespace std;
    int n,d;
int a[100100];
int ans[100100];
int check(long long now) {
    int ai = 1;
    long long nowh = 0;
    for(int i = 1;i <= d;i++) {
        nowh/=2;
        while(nowh < now) {
            if(ai <= n) {
                nowh+=a[ai];
                ans[ai] = i;
                ai++;
            } else {
                return 0;
            }
        }
    }
    while(ai <= n) {
        ans[ai] = d;
        ai++;
    }
    return 1;
}
int main(){
    scanf("%d%d",&n,&d);
    long long r = 0;
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
        r+=a[i];
    }
    long long l = 0;
    while(l <= r){
        long long mid = (l + r) / 2;
        if(check(mid)) {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
//    printf("~~%d!",check(1));
    check(r);
    printf("%lld\n",r);
    for(int i = 1;i <= n;i++) {
        printf("%d\n",ans[i]);
    }
}
/*
4 5
1
1
1
1
*/
View Code

E(bzoj1623)

#include<bits/stdc++.h>

using namespace std;

int a[100100];
priority_queue<int> q;
int main(){
    int n,m,d,l;
    scanf("%d%d%d%d",&n,&m,&d,&l);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
        if(a[i] < l){
            a[i] = -1;continue;
        }
        a[i] = (a[i] - l) / d;
    }
    sort(a+1,a+1+n);
    for(int i = 1; i <= m;i++) {
        q.push(0);
    }
    int ans = 0;
    for(int i =1;i <= n;i++) {
        int top = -q.top();
        if(a[i] >= top) {
            q.pop();
            q.push(-top-1); 
            ans++;
        }
    }
    printf("%d\n",ans);
}
View Code

F(bzoj2563)

#include<bits/stdc++.h>

using namespace std;
long long num[200200];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++) {
        scanf("%lld",&num[i]);
        num[i] *= 2;
    }
    for(int i = 1;i <= m;i++) {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        num[u]+=w;
        num[v]+=w;
    }
    sort(num+1,num+1+n);
    long long ans = 0;
    for(int i = 1;i <= n;i++) {
        if(i % 2 == 1) {
            ans -= num[i];
        } else {
            ans += num[i] ;
        }
    }
    printf("%lld\n",ans/2);
}
View Code

G(bzoj1634)

#include<bits/stdc++.h>

using namespace std;
struct node{
    int t,d;
}; 
int cmp(node x,node y) {
    return x.t*2*y.d < y.t*2*x.d;
}
node no[200200];

int main(){
    int n;
    scanf("%d",&n);
    long long sumd = 0;
    long long ans = 0;
    for(int i = 1;i <= n;i++) {
        scanf("%d%d",&no[i].t,&no[i].d);
        sumd+=no[i].d;
    }
    sort(no+1,no+1+n,cmp);
    for(int i = 1;i <= n;i++) {
    //    cout << no[i].t << " " << no[i].d << endl;
        //
        
        sumd -= no[i].d;
        ans += no[i].t*sumd;ans += no[i].t*sumd;
    }
    printf("%lld\n",ans);
} 
View Code

 

posted @ 2021-02-15 22:08  19992147  阅读(69)  评论(0编辑  收藏  举报