总时间限制:
10000ms
单个测试点时间限制:
1000ms
内存限制:
262144kB
描述
超大型偶像团体HIHO314159总选举刚刚结束了。制作人小Hi正在复盘分析投票过程。
小Hi获得了N条投票记录,每条记录都包含一个时间戳Ti以及候选人编号Ci,代表有一位粉丝在Ti时刻投了Ci一票。
给定一个包含K名候选人集合S={S1, S2, ... SK},小Hi想知道从投票开始(0时刻),到最后一张票投出的时刻(max{Ti}),期间有多少时间得票最多的前K名候选人恰好是S中的K名候选人。
注意这里对前K名的要求是"严格"的,换句话说,S中的每一名候选人得票都要大于任何一名S之外的候选人。S集合内名次先后不作要求。
注:HIHO314159这个团体有314159名团员,编号是1~314159。
输入
第一行包含两个整数N和K。
第二行包含2N个整数:T1, C1, T2, C2, ... TN, CN。
第三行包含K个整数:S1, S2, ... SK。
对于30%的数据,1 ≤ N, K ≤ 100
对于60%的数据,1 ≤ N, K ≤ 1000
对于100%的数据, 1 ≤ N, K ≤ 314159 1 ≤ Ti ≤ 1000000 1 ≤ Ci, SK ≤ 314159
输出
一个整数,表示前K名恰好是S一共持续了多少时间。
样例输入
10 2 3 1 4 1 5 1 4 3 6 5 8 3 7 5 8 5 9 1 10 5 1 5
样例输出
3
只需要维护最大最小值即可
set大法:
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn=4e5+10;
const int tot=314159;
struct T{
int t,c;
}a[maxn];
int mp[maxn];
multiset<int> s1,s2;
int n,k,s[maxn],m,ans;
bool in[maxn];
bool cmp(T x,T y){
return x.t<y.t;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i].t>>a[i].c;
for(int i=1;i<=k;i++) cin>>s[i],in[s[i]]=1;
sort(a+1,a+1+n,cmp);
if(k==tot){
cout<<a[n].t<<endl;
return 0;
}
for(int i=1;i<n;i++){
if(mp[a[i].c]){
if(in[a[i].c]) s1.erase(s1.find(mp[a[i].c]));
else s2.erase(s2.find(mp[a[i].c]));
}
mp[a[i].c]++;
if(in[a[i].c]) s1.insert(mp[a[i].c]);
else s2.insert(mp[a[i].c]);
int mn=0,mx=0;
if(s1.size()==k) mn=*s1.begin();
if(s2.size()) mx=*s2.rbegin();
ans+=(mn>mx)*(a[i+1].t-a[i].t);
}
cout<<ans<<endl;
return 0;
}
数组大法:
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn=4e5+10;
const int tot=314159;
struct T{
int t,c;
}a[maxn];
int mp[maxn],cnt[maxn],mn,mx;
multiset<int> s1,s2;
int n,k,s[maxn],ans;
bool in[maxn];
bool cmp(T x,T y){
return x.t<y.t;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i].t>>a[i].c;
cnt[0]=k;
for(int i=1;i<=k;i++) cin>>s[i],in[s[i]]=1;
sort(a+1,a+1+n,cmp);
if(k==tot){
cout<<a[n].t<<endl;
return 0;
}
for(int i=1;i<n;i++){
if(in[a[i].c]){
cnt[mp[a[i].c]]--;
mp[a[i].c]++;
cnt[mp[a[i].c]]++;
while(!cnt[mn]) mn++;
}
else{
mp[a[i].c]++;
mx=max(mx,mp[a[i].c]);
}
ans+=(mn>mx)*(a[i+1].t-a[i].t);
}
cout<<ans<<endl;
return 0;
}