首页 > 技术文章 > hdu 5090 数列贪心加成1~n

zibaohun 2014-11-02 17:08 原文

http://acm.hdu.edu.cn/showproblem.php?pid=5090

给一段长度为n数列,问能否给任意个数加上k的倍数,使得加完之后恰好只有1~n


贪心,先排序,依次加出1~n,每次看能否从给定数列中找到一个未被标记的数使得其能加到当前的i,能则标记,最后能完成即输出Jerry

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
const int modo = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int inf = 0x3fffffff;
const LL _inf = 1e18;
const int maxn = 105,maxm = 10005;
int p[maxn],n,k;
bool vis[maxn];
void work()
{
    clr0(vis);
    RD2(n,k);
    for(int i = 1;i <= n;++i)
        RD(p[i]);
    sort(p+1,p+n+1);
    for(int i = 1;i <= n;++i){
        bool flag = false;
        for(int j = 1;j <= n && p[j] <= i;++j){
            if(!vis[j] && i - p[j] >= 0 && (i - p[j]) % k == 0){
                flag = true;
                vis[j] = true;
                break;
            }
        }
        if(!flag){
            puts("Tom");
            return ;
        }
    }
    puts("Jerry");
    return;
}
int main()
{
    int _;RD(_);
    while(_--){
        work();
    }
    return 0;
}


推荐阅读