您的位置:首页 > 编程语言 > C语言/C++

UVA11729_Commando War

2016-01-19 15:26 387 查看
基本的贪心算法

之前把要求输出的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
4000
, curTime + timePoint[j].workTime);
}
cout << "Case " << count++ << ": " << tarTime << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 算法 uva