Codeforces Round #481 (Div. 3)

978A 水题
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int vis[1010];
int vv[1010];
int n;
int a[1010];

int main(){
    cin>>n;
    for(int i = 1; i<=n; i++){
        cin>>a[i];
    }
    int m = n;
    for(int i = n; i>=1; i--){
        if(!vis[a[i]]){
            vv[i] = 1;

            vis[a[i]] = 1;
        }
        else m--;
    }
    bool flag = false;
    printf("%d\n", m);
    for(int i = 1; i<=n; i++){
        if(vv[i]){
            if(flag == true) printf(" ");
            printf("%d", a[i]);
            flag = true;
        }
    }
    printf("\n");

    return 0;
}

978B 水题
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int n;
char s[110];

int num[110];

int main(){
    int cnt = 0;
    cin>>n;
    scanf("%s", s+1);
    bool flag = false;
    for(int i = 1; i<=n; i++){
        if(s[i] == 'x'){
            if(flag == false){
                cnt++;
                num[cnt] = 1;
            }
            else{
                num[cnt]++;
            }
            flag = true;
        }
        else{
            flag = false;
        }
    }
    int res = 0;
    for(int i = 1; i<=cnt; i++){
        if(num[i]>=3){
            num[i]-=2;
            res += num[i];
        }

    }
    cout<<res<<endl;

    return 0;
}

978C (二分)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int n, m;
long long a[2*100010];
long long pre[2*100010];
long long b[2*100010];

int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i<=n; i++){
        scanf("%I64d", &a[i]);
        pre[i] = pre[i-1]+a[i];
    }
    for(int i = 1; i<=m; i++){
        scanf("%I64d", &b[i]);
    }

    for(int i = 1; i<=m; i++){
        int pos = lower_bound(pre+1, pre+1+n, b[i])-pre;
        cout<<pos<<" "<<b[i]-pre[pos-1]<<endl;
    }


    return 0;
}

978E (细节题)
题意:有n个车站,有一辆容量为w的车,依次经过这n个车站,并给出经过每个车站车内人数的变化,问初始时车内的人数有几种可能。
思路:即车内初始人数不能大于w,且不能小于图中车内人数的最小值的绝对值,分类讨论一下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int n;
long long w;
long long a[1010];

int main(){
    scanf("%d%I64d", &n, &w);
    for(int i = 1; i<=n; i++){
        scanf("%I64d", &a[i]);
    }
    int ma = -0x3f3f3f3f;
    int mi = 0x3f3f3f3f;
    int sum = 0;
    for(int i = 1; i<=n; i++){
        sum+=a[i];
        ma = max(ma, sum);
        mi = min(mi, sum);
    }
    int res;
    if(mi<=0 && (-mi)>w){
        cout<<0<<endl;
        return 0;
    }
    if(ma>w || mi>w){
        cout<<0<<endl;
        return 0;
    }
    if(mi<=0){
        if(ma>=0) res = w-ma+1+mi;
        else res = w+mi+1;
        if(res<=0){
            cout<<0<<endl;
            return 0;
        }
        cout<<res<<endl;
        return 0;
    }
    else{
        res = w-ma+1;
        if(res<=0){
            cout<<0<<endl;
            return 0;
        }
        cout<<res<<endl;
        return 0;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值