P4875 改掉WA就行了 T我自行解决
#include<bits/stdc++.h>
using namespace std;
#define hash ___A__
const int N=1e5+5;
int n,m,ans=-1;
struct cow{
int pos,col;
friend int operator<(cow x,cow y){
return x.pos<y.pos;
}
}a[N];
int count(int x){
int ret=0;
while(x){
ret+=x%2,x/=2;
}
return ret;
}
int calc(int S){
map<vector<int>,int>mp;
vector<int>cnt(9,0),hash(9,0);
mp[hash]=a[1].pos;
int ret=-1,first=0;
for(int i=1;i<=n;i++){
cnt[a[i].col]++;
if(!S>>(a[i].col-1)&1){
hash[a[i].col]++;
}
else{
if(!first){
first=a[i].col;
}
for(int i=1;i<=8;i++){
if(S>>(i-1)&1){
hash[i]=cnt[first]-cnt[i];
}
}
}
vector<int>need(9,0);
for(int i=1;i<=8;i++){
if(S>>(i-1)&1){
if(!cnt[i]){
need[i]=-1;
}
else{
need[i]=cnt[first]-cnt[i];
}
}
else{
need[i]=cnt[i];
}
}
if(mp.find(need)!=mp.end()){
ret=max(ret,a[i].pos-mp[need]);
}
if(mp.find(hash)==mp.end()){
mp[hash]=a[i+1].pos;
}
}
return ret;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i].pos>>a[i].col;
}
sort(a+1,a+n+1);
for(int i=0;i<(1<<8);i++){
if(count(i)>=m){
ans=max(ans,calc(i));
}
}
cout<<ans;
}