BZOJ 5108: [CodePlus2017]可做题 贪心

code: 

#include <cstdio>  
#include <cstring>
#include <algorithm> 
#define N 100020  
#define ll long long 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;
struct data 
{
    int p,v; 
    bool operator<(data a) const { return p<a.p; } 
}a[N];  
int s[N],cnt[31],tot; 
int main() 
{ 
    // setIO("input"); 
    int n,m,i,j,c;    
    ll ans=0; 
    scanf("%d%d",&n,&m);  
    for(i=1;i<=m;++i) scanf("%d%d",&a[i].p,&a[i].v);   
    sort(a+1,a+1+m); 
    for(c=1;c<=m;++c) 
    {
        tot=1,s[1]=a[c].v;         
        while(c<m&&a[c+1].p-a[c].p==1) 
            ++tot,s[tot]=a[++c].v^s[tot-1];   
        if(!(c-tot)&&a[1].p==1) 
        {
            for(i=1;i<=tot;++i) ans+=s[i]; 
        }
        else
        {
            for(i=0;i<30;++i) 
            {
                cnt[i]=0; 
                for(j=1;j<=tot;++j) 
                {
                    if(s[j]&(1<<i))   ++cnt[i]; 
                }
                ans+=(1ll<<i)*min(cnt[i],tot-cnt[i]+1);   
            }
        }
    }
    printf("%lld\n",ans); 
    return 0; 
}

  

posted @ 2019-12-23 18:49  EM-LGH  阅读(115)  评论(0编辑  收藏  举报