跳到正文

题解

『CLMP OI Round 1 - T1』盗窃数据 题解

bz02约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年2月27日
更新
2026年2月27日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

题目传送门

题目传送门

题解

特殊性质A

容易发现,该性质最优是输出一个数,且该数是输出出现最多的数字。

特殊性质B

可以发现,最优解必为特判每个第二个数可能,然后对于每个特判,输出满足第二个数的数中输出出现最多的数字。

即:

C++
#include<bits/stdc++.h>
using namespace std;
long long x,y;
int main(){
    cin>>x>>y;
    if(y==1){
        cout<<第二个数=1中输出出现最多的数字<<endl;
    }
    else if(y==2){
        cout<<第二个数=2中输出出现最多的数字<<endl;
    }
    ......
    else{
        cout<<第二个数未知中输出出现最多的数字<<endl;
    }
  	return 0;
}

不给出代码。

100分(AC)

根据特殊性质B延申,可以发现,我们要特判每个第一、第二输入数不同的数,然后输出其中输出数最多的那个输出数。

即:

C++
#include<bits/stdc++.h>
using namespace std;
long long x,y;
int main(){
    cin>>x>>y;
    if(x==1){
        if(y==1){
            cout<<第一个数=1且第二个数=1中输出出现最多的数字<<endl;
        }
        else if(y==2){
            cout<<第一个数=1且第二个数=2中输出出现最多的数字<<endl;
        }
        ......
        else{
            cout<<第一个数=1且第二个数未知中输出出现最多的数字<<endl;
        }
    }
    else if(x==2){
        ......
    }
    ......
    else{
        ......
    }
  	return 0;
}

代码见下:

C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,opt,xx,kk,d[1000006],c[1000006],cd=0,op=0,os=0,cc=0,ce=0;
bool bo[1000006];
struct one{
    long long x,y,z;
}a[1000006],b[1000006];
map<pair<long long,long long>,long long> mp;
bool cmp(one a1,one b1){
    if(a1.x!=b1.x){
        return a1.x<b1.x;
    }
    else if(a1.y!=b1.y){
        return a1.y<b1.y;
    }
    else{
        return a1.z<b1.z;
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        a[i].x=1e9+7;
        a[i].y=1e9+7;
        a[i].z=1e9+7;
    }
    while(m--){
        cin>>opt>>xx>>kk;
        for(int i=1;i<=kk;i++){
            cin>>d[i];
        }
        if(opt==1){
            for(int i=1;i<=kk;i++){
                a[d[i]].x=xx;
            }
        }
        else if(opt==2){
            for(int i=1;i<=kk;i++){
                a[d[i]].y=xx;
            }
        }
        else{
            for(int i=1;i<=kk;i++){
                a[d[i]].z=xx;
                if(bo[d[i]]==0){
                    c[++cd]=d[i];
                    bo[d[i]]=1;
                }
            }
        }
    }
    for(int i=1;i<=cd;i++){
        b[i]=a[c[i]];
    }
    sort(b+1,b+cd+1,cmp);
    //cout<<cd<<endl;
    for(int i=1;i<=cd;i++){
        if(i==1||b[i].x!=b[i-1].x||b[i].y!=b[i-1].y){
            if(i==1){
                os=i;
            }
            else{
                cc=0;
                ce=0;
                for(int j=os;j<=i-1;j++){
                    if(j!=os&&b[j].z!=b[j-1].z){
                        cc=max(cc,ce);
                        ce=1;
                    }
                    else{
                        ce++;
                    }
                }
                cc=max(cc,ce);
                op+=cc;
            }
            os=i;
        }
    }
    cc=0;
    ce=0;
    for(int j=os;j<=cd;j++){
        if(j!=os&&b[j].z!=b[j-1].z){
            cc=max(cc,ce);
            ce=1;
        }
        else{
            ce++;
        }
    }
    cc=max(cc,ce);
    op+=cc;
    cout<<op<<endl;
	return 0;
}
原文