当前位置: 首页 > 工具软件 > Commando > 使用案例 >

UVA11729 Commando War 贪心

田兴怀
2023-12-01

题意翻译
突击战

你有n个部下,每个部下需要完成一项任务。第i个部下需要你花Bj分钟交代任务,然后他就会立刻独立地、无间断地执行Ji分钟后完成任务。你需要选择交代任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任务应尽早结束)。注意,不能同时给两个部下交代任务,但部下们可以同时执行他们各自的任务。
输入格式

输入包含多组数据,每组数据的第一行为部下的个数N(1<=N<=1000);以下N行每行两个正整数B和J(1<=B<=10000,1<=J<=10000),即交待任务的时间和执行任务的时间。输入结束标志为N=0。
输出格式

对于每组数据,输出所有任务完成的最短时间。
样例输入

3 2 5 3 2 2 1 3 3 3 4 4 5 5 0
样例输出

Case 1:8 Case 2:15

由 @Legends丶dream 提供翻译
输入输出样例
输入 #1

3
2 5
3 2
2 1
3
3 3
4 4
5 5
0

输出 #1

Case 1: 8
Case 2: 15

贪心 : 执行时间长的先交代并执行
即 : 按执行时间从大到小排序
证明在刘汝佳蓝书第4

struct Node {
	int x/*交代时间x*/, y/*执行时间y*/;
	bool operator < (const Node& no) const {
		return y > no.y; //按执行时间从大到小排序
	}
} a[MAXN];

完整代码

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...)                             \
    do {                                       \
        cout << "\033[31;1m " << #x << " -> "; \
        err(x);                                \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

struct Node {
	int x/*交代时间x*/, y/*执行时间y*/;
	bool operator < (const Node& no) const {
		return y > no.y; //按执行时间从大到小排序
	}
} a[MAXN];

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	int cas = 0;
	while((read(n), n)) {
		for(int i=1; i<=n; i++)
			read(a[i].x, a[i].y);
		sort(a+1, a+1+n); //排序
		int start = 0, ans = 0;
		for(int i=1; i<=n; i++) {
			start += a[i].x; //开始执行任务i
			ans = max(ans, start+a[i].y); //记录最小答案
		}
		printf("Case %d: %d\n", ++cas, ans);
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}

/**
                                                               
 ,]]]    ,]]] ]]]`   ,]]].  .]/@@\]. ,]].    ]]]  ,]]]]]]`     
  =@@\  ,@@@.  \@@\ =@@/. ,@@@@@@@@` =@@@\   @@@  =@@   @@@^   
   ,@@@,@@@.    =@@@@@/  ,@@@.       =@@@@@` @@@  =@@   /@@^   
    ,@@@@/       =@@@^   =@@^        =@@^,@@\@@@  =@@@@@@@@\.  
     .@@@       /@@@@@@. ,@@@.       =@@^ .@@@@@  =@@    =@@^  
     .@@@     ,@@@` ,@@@` =@@@@@@@@^ =@@^   \@@@  =@@@@@@@@@   
     .[[[    .[[[.   ,[[[`  ,[@@@/[. ,[[`    ,[[  ,[[[[[[`     
                                                                   
*/

 类似资料: