【HDU5900 2016 ACM ICPC Asia Regional Shenyang Online I】【区间DP】QSC and Master 相邻互质取数最大取值.cpp

来源:互联网 发布:风行网络电影 编辑:程序博客网 时间:2024/05/22 06:37

Eighty seven

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 686    Accepted Submission(s): 241


Problem Description
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare Ncards with numbers. The number on the i-th card is ai. In class, each turn he will remove no more than 3 cards and let students choose any ten cards, the sum of the numbers on which is 87. After each turn the removed cards will be put back to their position. Now, he wants to know if there is at least one solution of each turn. Can you help him?
 

Input
The first line of input contains an integer t (t5), the number of test cases. t test cases follow.
For each test case, the first line consists an integer N(N50).
The second line contains N non-negative integers a1,a2,...,aN. The i-th number represents the number on the i-th card. The third line consists an integer Q(Q100000). Each line of the next Q lines contains three integers i,j,k, representing Mr.Fib will remove the i-th, j-th, and k-th cards in this turn. A question may degenerate while i=j, i=k or j=k.
 

Output
For each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.
 

Sample Input
1121 2 3 4 5 6 7 8 9 42 21 22101 2 33 4 52 3 210 10 1010 11 1110 1 11 2 101 11 121 10 1011 11 12
 

Sample Output
NoNoNoYesNoYesNoNoYesYes
 

Source
2016 ACM/ICPC Asia Regional Qingdao Online

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 303, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }int casenum, casei;int n;int key[N], val[N];bool pick[N][N];int gcd(int x, int y){return y == 0 ? x : gcd(y, x%y);}LL sum[N];LL f[N][N];LL dp(){for (int l = 1; l <= n; ++l)for (int r = l; r <= n; ++r)f[l][r] = 0;for (int len = 1; len < n; ++len){for (int l = 1; l + len <= n; ++l){int r = l + len;for (int i = l; i < r; ++i)gmax(f[l][r], f[l][i] + f[i + 1][r]);if (pick[l][r] && f[l + 1][r - 1] == sum[r - 1] - sum[l]){gmax(f[l][r], f[l + 1][r - 1] + val[l] + val[r]);}}}return f[1][n];}int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d", &n);for (int i = 1; i <= n; ++i)scanf("%d", &key[i]);for (int i = 1; i <= n; ++i)scanf("%d", &val[i]), sum[i] = sum[i - 1] + val[i];for (int i = 1; i <= n; ++i){for (int j = i; j <= n; ++j){pick[i][j] = (gcd(key[i], key[j]) > 1);}}printf("%lld\n", dp());}return 0;}/*【题意】给你n个数,组成一条链。如果现有的数中,有2个相邻的数的权值gcd > 1,则我们可以取走这2个数,得到它们的价值问你最优取数方案下的最大得分【类型】区间DP【分析】1,我们可以求出哪两个数的gcd>1,表示它们可以同时取2,然后我们做区间DP对于区间f[l][r],我们枚举其合并方式,可以是两端合并(有要求),也可以是中间合并。更新一个最大值【时间复杂度&&优化】O(n^3)*/


0 0
原创粉丝点击