1) 3420题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3420
题目大意:车票为1KM的票,下车之后可以给其他人,现有n给人乘车,问最少多少钱的票就够了。
AC代码
#include<iostream>
#include<algorithm>
#define N 1005
using namespace std;
int main()
{
int n,KM[N];
while(cin>>n){
for(int i=0;i<n;i++){
cin>>KM[i];
}
int price=0,coin=0;
sort(KM,KM+n);
for(int i=0;i<n;i++){
price=KM[i]*(n-i);
if(price>coin) coin=price;
}
cout<<coin<<endl;
}
return 0;
}
**2)**3090题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3090
题目大意:若某人回家有N条路,TA有M个钱币,每条路上每一千米有P个强盗,TA可以用钱请保镖来保护TA不被抢劫(在哪里请保镖由TA自己决定),若不请保镖则会在每一千米被该千米内的每一个强盗抢劫一个钱币。现给定N,M,P,及每条路的长度D,问TA 最少被抢多少钱。
思路——贪心:按照强盗数从大到小进行排序。
AC代码
#include<iostream>
#include<algorithm>
#define P 10005
using namespace std;
struct Node
{
int length;
int robber;
};
bool compare(const Node &a,const Node &b)
{
return a.robber > b.robber;
}
int main()
{
Node road[P];
int N,total;
__int64 M;
while(cin>>N>>M&&(N!=0)||(M!=0)){
total = 0;
for(int i=0;i<N;i++){
cin>>road[i].length>>road[i].robber;
total+=road[i].length*road[i].robber;
}
sort(road,road+N,compare);
for(int i=0;i<N;i++){
if(M>road[i].length){
M-=road[i].length;
total-=road[i].length*road[i].robber;
}
else{
total-=M*road[i].robber;
break;
}
}
cout<<total<<endl;
}
return 0;
}
3)
There is a war and it doesn’t look very promising for your country. Now it’s time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-plan, every single soldier has a unique responsibility and you don’t want any of your soldier to know the plan for other soldiers so that everyone can focus on his task only. In order to enforce this, you brief every individual soldier about his tasks separately and just before sending him to the battlefield. You know that every single soldier needs a certain amount of time to execute his job. You also know very clearly how much time you need to brief every single soldier. Being anxious to finish the total operation as soon as possible, you need to find an order of briefing your soldiers that will minimize the time necessary for all the soldiers to complete their tasks. You may assume that, no soldier has a plan that depends on the tasks of his fellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing in between.
Input
There will be multiple test cases in the input file. Every test case starts with an integer N (1<=N<=1000), denoting the number of soldiers. Each of the following N lines describe a soldier with two integers B (1<=B<=10000) & J (1<=J<=10000). B seconds are needed to brief the soldier while completing his job needs J seconds. The end of input will be denoted by a case with N =0 . This case should not be processed.
Output
For each test case, print a line in the format, “Case X: Y”, where X is the case number & Y is the total number of seconds counted from the start of your first briefing till the completion of all jobs.
Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
0
Sample Output
Case 1: 8
Case 2: 15
题目大意:你有n个部下,每个需要去完成一件任务,你给每个部下布置任务的时间是B,部下完成该任务的时间是J,每次只能给一个人布置任务,但每个部下完成任务时互不影响,可同时进行,需要去选择一种方案来使完成所有任务的时间最少。得到的最短时间即输出。
思路——贪心:完成任务所需时间长的先交代。按照J从大到小的顺序给任务排序。
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
struct Time
{
int B;
int J;
};
bool cmp(const Time &a,const Time &b)
{
return a.J>b.J;
}
int main()
{
int N,total,cnt=0;
while(cin>>N&&N){
Time sec[1005];
cnt++;
for(int i=0;i<N;i++){
cin>>sec[i].B>>sec[i].J;
}
sort(sec,sec+N,cmp);
total=0;
int maxT=0,tempt=0;
for(int i=0;i<N;i++){
tempt+=sec[i].B;
if(sec[i].J+tempt>maxT) maxT=sec[i].J+tempt;
}
cout<<"Case "<<cnt<<": "<<maxT<<endl;
}
return 0;
}