标签:
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168
分析:找到一天中需要最多的房间即可
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 200 int day[N];//day[i] 第i天的最多房间数 int main() { freopen("d:\\in.txt", "r", stdin); int t, n; scanf("%d", &t); while(t--){ memset(day, 0, sizeof(day)); scanf("%d", &n); int num, start, length; for(int i=0; i<n; i++){ scanf("%d%d%d", &num, &start, &length); for(int i=0; i<length; i++){ day[i+start] += num; } } int ans = 0; for(int i=0; i<N; i++){ ans = max(ans, day[i]); } printf("%d\n", ans); } return 0; }
看别人题解后才明白的,同时记录下自己的模拟算法(TimeLimitExceeded)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 #define INPUT freopen("d:\\in.txt", "r", stdin); 9 #define N 10002 10 11 struct Node { 12 int a, b, v; 13 }c[N]; 14 15 bool cmp(Node n1, Node n2) 16 { 17 return n1.a != n2.a ? n1.a < n2.a : n1.b < n2.b; 18 } 19 int main() 20 { 21 INPUT; 22 23 int t, n; 24 scanf("%d", &t); 25 while(t--) 26 { 27 scanf("%d", &n); 28 int day; 29 for(int i=0; i<n; i++){ 30 scanf("%d%d%d", &c[i].v, &c[i].a, &day); 31 c[i].b = c[i].a+day; 32 } 33 sort(c, c+n, cmp); 34 int ans = c[0].v; 35 for(int i=1; i<n; i++){ 36 int cur = c[i].v; 37 ans += cur; 38 for(int j=0; j<i; j++){ 39 if(c[j].v && cur && c[j].b <= c[i].a){//如果 j团有剩余房间 且 当前订单还需要房间 且j团已离开 40 int reuse = min(c[j].v, cur); 41 ans -= reuse; 42 c[j].v -= reuse; 43 cur -= reuse; 44 } 45 } 46 } 47 printf("%d\n", ans); 48 } 49 return 0; 50 }
[河南省ACM省赛-第三届] 房间安排 (nyoj 168)
标签:
原文地址:http://www.cnblogs.com/vegg117/p/4387772.html