UVA11729_Commando War

基本的貪心算法

之前把要求輸出的Case xxx寫成小寫case,一直沒發現..結果都是wa,氣炸了。所以,做題一定要細心來



#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#define MAX(a,b) (a)>(b)?(a):(b)
using namespace std;

struct TimePoint{
	int speakTime;
	int workTime;
};
vector<TimePoint> timePoint;

bool cmp(TimePoint p1, TimePoint p2){
	return p1.workTime >= p2.workTime;
}
int main()
{
	int n,count=1;
	int i,curTime,tarTime;
	while (cin>>n && n!=0){
		i = curTime = tarTime= 0;		//initialize
		timePoint.clear();
		while (n--){
			TimePoint point;
			scanf("%d %d", &point.speakTime, &point.workTime);
			timePoint.push_back(point);
		}
		sort(timePoint.begin(), timePoint.end(), cmp);


		for (int j = 0; j < timePoint.size(); j++){
			curTime += timePoint[j].speakTime;
			tarTime = MAX(tarTime, curTime + timePoint[j].workTime);
		}
		cout << "Case " << count++ << ": " << tarTime << endl;
	}

	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章