2020CSPT2

#include <bits/stdc++.h>
using namespace std;

const int M=1E5+50;
struct stu{
    int id;
    int flag=0;//用flag来帮忙排序
    int cnt1=0;
    int cnt0=0;
};
stu st[M];
int n;
bool cmp(stu&i,stu&j){
    return i.flag>j.flag;
}
int cnt;
int zero[M];//前缀和
int one[M];//后缀和
int now1;
int ans;
int maxn;
int main(){
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int id,flag;
        cin>>id>>flag;
        st[id].flag=1;
        st[id].id=id;
        if(flag){
            st[id].cnt1++;
        }
        else {
            st[id].cnt0++;
        }
    }
    sort(st+1,st+1+n,cmp);

   for(int i=1;i<=n;i++){
        one[i]=cnt-now1;
        zero[i]=zero[i-1]+st[i-1].cnt0;
        if(one[i]+zero[i]>=maxn) {
            maxn = one[i] + zero[i];
            ans = st[i].id;
        }
            now1+=st[i].cnt1;
    }
    cout<<ans;


    return 0;
}

还有个需要离散化的步骤,但是没时间了,想吃饭了。
就写到到这里吧

upd

实验课重新写了一个

#include <bits/stdc++.h>
using namespace std;
const int M=1e5+50;
struct stu{
    int id;
    int flag=1;
};
stu st[M];
int zero[M];//前缀
int one[M];//后缀
bool cmp(stu&i,stu&j){
    if(i.id!=j.id){
        return i.id<j.id;
    }
    return i.flag>j.flag;
}
int n;
int cnt1;//1总数
int now0;
int now1;
int maxn;
int ans;

int main(){
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>st[i].id>>st[i].flag;
        if(st[i].flag){
            cnt1++;
        }
    }
    sort(st+1,st+1+n,cmp);


    for(int i=1;i<=n;i++){
        zero[i]=now0;
        one[i]=cnt1-now1;
        if(zero[i]+one[i]>=maxn){
            maxn=zero[i]+one[i];
            ans=st[i].id;
        }
        if(st[i].flag){
            now1++;
        }
        else{
            now0++;
        }
    }
    cout<<ans;


    return 0;
}

这个最主要的思路就是排序的时候要把1排在前面,0排在后面,这样算的时候才会正确,反之的话就会出现算 0 出现错误

其实最常见的思路还是用离散化那种23333,由此也可以看出掌握常见的套路有多重要233333

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值