ACM-ICPC ShangHai 2014

A.

    题意:给定一个序列,可以把里面的任意几个数加上K的整数倍,变换之后要求把这个序列从小到大排后变为 数列1,2,3,4,5,6

 

    解答:最直观的方法是个匹配,但觉得500*100*100*100可能会超

             不过可以发现A可以变为B only when A%K == B%K && A <= K

     那么对每个可能的余数开个优先队列扫一遍就可以啦

 

 

    要被自己蠢死了QAQ

    没清空完就break出QAQ

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>

using namespace std;

const int N = 105;


typedef struct node{ 
    
    int xi,v;
    
    bool operator < (const node &a)const{ 
           if (xi == a.xi) return v < a.v;
         else return  xi > a.xi;
    }

}node;

node newnode(int x,int y){ 
    node tmp;
    tmp.xi = x;
    tmp.v = y;
    return tmp;
}

int n,m,k;

int a[N],b[N],tmpa[N],tmpb[N]; 

priority_queue<node>Q[N];


int main(){

    int T; cin >> T;

    while (T--){ 
         
         cin >> n >> k;

         for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
         
         for (int i = 1;i <= n;i++) b[i] = i;
   
        for (int i = 1;i <= n;i++) Q[a[i]%k].push(newnode(a[i],1));

        for (int i = 1;i <= n;i++) Q[b[i]%k].push(newnode(b[i],-1));
       
        int flag = 0; int cnt = 0;

        for (int t = 0;t < k;t++,cnt = 0){
          while(!(Q[t].empty())){ 
                  node u = Q[t].top();
                  Q[t].pop();
                  cnt += u.v;
                  if (cnt < 0) flag = 1;
        }
        
        if (cnt != 0) flag = 1; 
        
        }
        
        if (flag) printf("Tom\n");
            else printf("Jerry\n");

    }
    
}

 

posted @ 2015-09-25 20:20  william's blog  阅读(156)  评论(0编辑  收藏  举报

adopt your own virtual pet!