任务

看了很久并没有懂,以下是标程的分析。

 

好难啊啊啊啊啊

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <algorithm>
 6 #include <cstdlib>
 7 #include <stack>
 8 using namespace std;
 9 
10 const int maxn = 3005;
11 int a[maxn],b[maxn],dp[2][maxn*2];
12 #define inf 0x3f3f3f3f
13 int main() {
14     int t;
15     scanf("%d",&t);
16     while(t--) {
17         int n;
18         scanf("%d",&n);
19         for(int i = 1 ; i <= n ; i ++) scanf("%d %d",&a[i],&b[i]);
20         int off = 3000;
21         memset(dp,inf,sizeof dp);
22         dp[0][3000] = 0;
23         int k = 0;
24         for(int i = 1; i <= n ; i ++) {
25             for(int j = 0 ; j < 6001 ; j ++) {
26                 if(dp[k][j] != inf) {
27                     if(j > off) {
28                         dp[1-k][off+a[i]] = min(dp[k][j]+a[i],dp[1-k][off+a[i]]);
29                         dp[1-k][j-b[i]] = min( dp[1-k][j-b[i]], dp[k][j] + max(0,off-j+b[i]) );
30                     }
31                     else {
32                         dp[1-k][j +a[i] ] = min(dp[1-k][j+a[i]] ,dp[k][j] + max(0,j+a[i]-off));
33                         dp[1-k][off-b[i]] = min(dp[1-k][off-b[i]] ,dp[k][j]+b[i]);
34 
35                     }
36                 }
37             }
38             memset(dp[k],inf,sizeof dp[k]);
39             k = 1 - k;
40         }
41         int ans = inf ;
42         for(int i = 0 ; i <= 6000; i++) {
43             if(dp[k][i] != inf) {
44                 ans = min(ans,dp[k][i] );
45             }
46         }
47         printf("%d\n",ans);
48     }
49     return 0;
50 }

大佬的:

 1 //
 2 //  Created by TaoSama on 2016-05-17
 3 //  Copyright (c) 2016 TaoSama. All rights reserved.
 4 //
 5 #pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <algorithm>
 7 #include <cctype>
 8 #include <cmath>
 9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12 #include <ctime>
13 #include <iomanip>
14 #include <iostream>
15 #include <map>
16 #include <queue>
17 #include <string>
18 #include <set>
19 #include <vector>
20 
21 using namespace std;
22 #define pr(x) cout << #x << " = " << x << "  "
23 #define prln(x) cout << #x << " = " << x << endl
24 const int N = 2e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
25 const int OFF = 3000;
26 
27 int n, a[N], b[N];
28 int f[2][OFF * 2 + 5]; //前i个任务 且A比B多用时j 的最小时间花费
29 
30 void getMin(int& x, int y) {
31     if(x > y) x = y;
32 }
33 
34 int main() {
35 #ifdef LOCAL
36     freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
37 //  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
38 #endif
39     ios_base::sync_with_stdio(0);
40     clock_t _ = clock();
41 
42     int t; scanf("%d", &t);
43     while(t--) {
44         scanf("%d", &n);
45         for(int i = 1; i <= n; ++i) scanf("%d%d", a + i, b + i);
46         int z = 0;
47         memset(f[z], 0x3f, sizeof f[z]);
48         f[z][OFF] = 0;
49         for(int i = 1; i <= n; ++i) {
50             memset(f[!z], 0x3f, sizeof f[!z]);
51             for(int j = -OFF; j <= OFF; ++j) {
52                 if(j > 0) {
53                     getMin(f[!z][a[i] + OFF], f[z][j + OFF] + a[i]); //给A B只能等
54                     getMin(f[!z][j + OFF - b[i]], f[z][j + OFF] + max(0, b[i] - j));
55                 } else {
56                     getMin(f[!z][-b[i] + OFF], f[z][j + OFF] + b[i]); //给B A只能等
57                     getMin(f[!z][j + OFF + a[i]], f[z][j + OFF] + max(0, a[i] + j));
58                 }
59             }
60             z = !z;
61         }
62         int ans = *min_element(f[z], f[z] + 2 * OFF + 1);
63         printf("%d\n", ans);
64     }
65 
66 #ifdef LOCAL
67     printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC);
68 #endif
69     return 0;
70 }

 

posted @ 2017-06-09 21:19  天之道,利而不害  阅读(193)  评论(0编辑  收藏  举报