【贪心】GCJ 2012 round1A kingdom rush

GCJ = Google Code Jam,是一种形式比较特殊的比赛,可以下载到测试数据,然后参赛者只需要提交输出文件,至于结果是如何得出来的并不重要,所以理论上说用肉眼去看亲自动笔算也是可以的。。。。

第一次接触,就看了一下往年的题目,round 1的前两题基本上可以招架住,第三题就超出能力所及了。。。。

kingdom rush也蛮简单的,用贪心法就好了,注意rating 2不合适时要选择对应rating 2最高的rating 1.

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
using namespace std;

//i think greedy algorithm does work.
//and it really does work!!
//choose for optimal ways to solve the problem!
//yup~~
int main(){
	freopen("large.in", "r", stdin);
	freopen("out.txt", "w", stdout);
	int t, n;
	pair<int, int> level[1005];
	int remain[1005];
	cin>>t;
	for(int i=0; i<t; i++){
		cin>>n;
		for(int j=0; j<1005; j++)
			remain[j] = 2;
		for(int j=0; j<n; j++)
			cin>>level[j].first>>level[j].second;
		int star = 0;
		int step = 0;
		//bool solution = true;
		while(star < 2*n){
			//look for 2-star rating until can not find
			int lower_bound;
			while(1){
				bool check = false;
				lower_bound = 1000000;
				for(int j=0; j<n; j++){
					if((remain[j]>0) && (star>=level[j].second)){
						star += remain[j];
						remain[j] = 0;
						step++;
						check = true;
					}
					else if(remain[j]>0)
						lower_bound = min(lower_bound, level[j].second);
				}
				if(check == false)
					break;
			}
			//look for one star rating
			//solution = false;

			//bool check = false;
			int tmp = -1;
			int index;
			for(int j=0; j<n; j++){
				if((remain[j]==2) && (star>=level[j].first)){
					if(level[j].second > tmp)
						index = j;
					tmp = max(tmp, level[j].second);
				}
			}
			if(tmp > -1){
				star++;
				remain[index]--;
				step++;
			}		
			else
				break;
//			}
//			if(star < lower_bound){
//				solution = false;
//				break;
//			}
		}
		//cout<<"star: "<<star<<endl;
		if(star < 2*n)
			cout<<"Case #"<<(i+1)<<": Too Bad"<<endl;
		else
			cout<<"Case #"<<(i+1)<<": "<<step<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值