您的位置:首页 > 其它

POJ 1323 Game Prediction 贪心

2015-10-15 15:19 260 查看
题目链接:http://poj.org/problem?id=1323

题目大意:m个人(包括你)玩一个卡片游戏,每个人n张卡片,卡片上数字为小于等于n * m的正整数,没有重复的数字。每轮比赛,每个人拿出一张牌,卡片上数值最大的那个人获得本轮胜利。输入m、n,接着输入你获得的每张卡片上的数字,接着输入一个空行,求你最少可以获得几次胜利。

题目解析:由于m,n较小,可以依次枚举你的每张卡片上的数值,找到一张在别人手里的牌,这个牌的数值最接近且大于它,并标记为使用过,若找不到,则证明你可以凭借本张牌在一轮比赛中获胜。

程序源码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAXN = 100010;
int card[MAXN];
bool flag[MAXN];
int main() {
int m, n;
int nIndex = 1;
while (cin >> m >> n && m && n) {
memset(flag, 0, sizeof(flag));
for (int i = 1; i <= n; i++) {
cin >> card[i];
flag[card[i]] = true;
}
getchar();getchar();
int ans = 0;
for (int i = 1; i <= n; i++) {
bool isGreat = false;
for (int j = 1; j <= n * m; j++) {
if (!flag[j]) {
if (j > card[i]) {
isGreat = true;
flag[j] = true;
break;
}
}
}
if (!isGreat) {
ans ++;
}
}
cout << "Case " << nIndex << ": " << ans << endl;
nIndex ++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Greedy