刷題#R5

星空
【問題描述】
你是能看到第一題的 friends 呢。 ——hja
點點星空是一張n× m的棋盤,左下角有顆星星。尤和千每次可以將星星向
右邊、右上、上邊移動一格。尤和千輪流移動,尤先手,問尤是否必勝?
【輸入格式】
多組數據,每行兩個整數n,m,當 n=m=0 時數據停止。
【輸出格式】
對於每組數據,如果尤必勝輸出“Yuri” ,否則輸出“Chito” 。
【樣例輸入】
5 3
5 4
6 6
0 0
【樣例輸出】
Chito
Yuri
Yuri
【數據範圍與規定】
50%的數據,1 ≤n,m≤ 10。
對於100%的數據,1 ≤ n,m≤ 1000。

這裏寫圖片描述
這裏寫圖片描述

T1
博弈題。
這裏寫圖片描述

第一圈綠色的點是必贏點,那麼只能到達這些綠色點的藍色的點是必輸點,然後,能夠到達藍色點的是必贏點,如此拓展。
可以看出,橫縱座標都是奇數時就是必輸點,否則就是必贏點。
T2
前40%暴力n^2
另外20%所有數不超過1023時用桶來優化(還是暴力)。
100%,01字典樹。
T3
線段樹,K<=10,維護區間前10大就可以了,常數可能有點大,重載運算符。

T1

#include<iostream> 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
int n,m;
int main()
{
    while(1)
    {
        scanf("%d%d",&n,&m);
        if(!n&&!m) break;
        if((n&1)&(m&1)) printf("Chito\n");
        else printf("Yuri\n");
    }
    return 0;
}

T2
60分

#include<iostream> 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int MOD=(1e9)+7;
const int N=5e4+7;
int n,k,a[N],m;
LL ans;
int num[15000],cnt[1500];
int main()
{
    freopen("war.in","r",stdin);
    freopen("war.out","w",stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    if(n<=100)
    {
        for(int i=1;i<=n;i++)
         for(int j=i+1;j<=n;j++)
          num[++m]=a[i]^a[j];
        sort(num+1,num+m+1);
        for(int i=m;i>m-k;i--)
         ans=(ans+num[i])%MOD;
    }
    else
    {
        int maxn=0;
        for(int i=1;i<=n;i++) maxn=max(maxn,a[i]),num[a[i]]++;

        for(int i=0;i<=maxn;i++)
         for(int j=i+1;j<=maxn;j++)
          cnt[i^j]+=num[i]*num[j];
        for(int i=1023;i>=0&&k;i--)
        {
            if(k>=cnt[i])
            {
                ans=(ans+1ll*cnt[i]*i)%MOD;
                k-=cnt[i];
            }
            else
            {
                ans=(ans+1ll*k*i)%MOD;
                k=0;
            }
        }
    }
    printf("%lld",ans);
    return 0;
} 

T3

#include<iostream> 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int N=100009;
const int K=10;
struct rec{
    int c[K+2];
    int add;
    rec operator + (const rec &b)const{
        rec p;
        int p1=0,p2=0;
        for(int i=0;i<K;i++)
         if(c[p1]>b.c[p2]) p.c[i]=c[p1++];
         else p.c[i]=b.c[p2++];
        p.add=0; 
        return p;
    }
}z[N*4+100];
int n,m,a[N];
void update(int o)
{
    z[o]=z[o<<1]+z[o<<1|1];
}
void push_down(int o)
{
    if(z[o].add){
        for(int i=0;i<K;i++)
        {
            if(z[o<<1].c[i])z[o<<1].c[i]+=z[o].add;
            if(z[o<<1|1].c[i])z[o<<1|1].c[i]+=z[o].add;
        }

        z[o<<1].add+=z[o].add;
        z[o<<1|1].add+=z[o].add;
        z[o].add=0;
    }
} 
void build(int o,int l,int r)
{
    if(l==r) 
    {
        z[o].c[0]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    update(o);
}
rec query(int o,int l,int r,int ql,int qr)
{
    if(l>qr||r<ql) return z[0];
    if(ql<=l&&qr>=r) return z[o];
    push_down(o);
    int mid=(l+r)>>1;
    if(ql<=mid)
    {
        if(qr>mid) return query(o<<1,l,mid,ql,qr)+query(o<<1|1,mid+1,r,ql,qr);
        else return query(o<<1,l,mid,ql,qr);
    }
    else return query(o<<1|1,mid+1,r,ql,qr);
}
void change(int o,int l,int r,int ql,int qr,int ad)
{
    if(r<ql||l>qr) return;
    if(l>=ql&&qr>=r) 
    {
        z[o].add+=ad;
        return;
    } 

    push_down(o);

    int mid=(l+r)>>1;
    if(mid>=ql) change(o<<1,l,mid,ql,qr,ad);
    if(mid<qr) change(o<<1|1,mid+1,r,ql,qr,ad);

    update(o);
}
int main()
{
    freopen("noname.in","r",stdin);
    freopen("noname.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=0;i<=4*n;i++)
    {
        z[i].add=0;
        for(int j=0;j<K;j++)
        z[i].c[j]=0;
    }
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int opt,l,r,x;
        scanf("%d%d%d%d",&opt,&l,&r,&x);
        if(opt==0)
        {
            if(r-l+1<x||!x) printf("-1\n");//k不合法
            else printf("%d\n",query(1,1,n,l,r).c[x-1]); 
        }
        else
        {
            change(1,1,n,l,r,x);
        }
    }
    return 0;
}
發佈了339 篇原創文章 · 獲贊 240 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章