1. 程式人生 > >POJ 1678 I Love this Game

POJ 1678 I Love this Game

first 先行者 logs 最大差值 sin return algo pre sstream

題目鏈接:http://poj.org/problem?id=1678

動態博弈。用dp[i]來表示如果先行者首先選擇第i個數字的話能取得的最大差值。由於每次選擇的數字一定比上一次選擇的數字大,所以先對數組進行排序。然後對於每個數字,如果先行者首先選擇這個數字的話,dp[i] 初始化的值為num[i].然後在num[i]之後的序列中,如果有符合條件的數字的話,那麽選擇dp值最大的那個數字。最後對所有的數字進行for loop,尋找符合條件的並且差值最大的數字。代碼如下:

 1 //============================================================================
2 // Name : test.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include <math.h> 11 #include <stdio.h> 12
#include <cstdio> 13 #include <algorithm> 14 #include <string.h> 15 #include <string> 16 #include <sstream> 17 #include <cstring> 18 #include <queue> 19 #include <vector> 20 #include <functional> 21 #include <cmath> 22 #include <set> 23
#define SCF(a) scanf("%d", &a) 24 #define IN(a) cin>>a 25 #define FOR(i, a, b) for(int i=a;i<b;i++) 26 #define Infinity 999999999 27 #define NInfinity -999999999 28 #define PI 3.14159265358979323846 29 typedef long long Int; 30 using namespace std; 31 32 int main() 33 { 34 int t; 35 int n, a, b; 36 SCF(t); 37 int num[10005]; 38 int dp[10005]; 39 while (t--) 40 { 41 SCF(n); 42 SCF(a); 43 SCF(b); 44 FOR(i, 0, n) 45 SCF(num[i]); 46 47 sort(num, num + n); 48 49 for (int i = n - 1; i >= 0; i--) 50 { 51 dp[i] = num[i]; 52 bool first = true; 53 int maxNum = 0; 54 for (int j = i + 1; j < n; j++) 55 { 56 int diff = num[j] - num[i]; 57 if (diff >= a && diff <= b) 58 { 59 if (first) 60 { 61 maxNum = dp[j]; 62 first = false; 63 } 64 else 65 maxNum = max(maxNum, dp[j]); 66 } 67 } 68 dp[i] = dp[i] - maxNum; 69 } 70 int maxAns = 0; 71 bool found = false; 72 for (int i = n - 1; i >= 0; i--) 73 { 74 if (num[i] >= a && num[i] <= b) 75 { 76 if (!found) 77 { 78 maxAns = dp[i]; 79 found = true; 80 } 81 else 82 { 83 if (dp[i] > maxAns) 84 maxAns = dp[i]; 85 } 86 } 87 } 88 printf("%d\n", maxAns); 89 } 90 return 0; 91 }

POJ 1678 I Love this Game