【贪心】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;
}


你可能感兴趣的:(【贪心】GCJ 2012 round1A kingdom rush)