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
N
cards 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 (t≤5)
, the number of test cases.
t
test cases follow.
For each test case, the first line consists an integer N(N≤50) .
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(Q≤100000) . 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 .
For each test case, the first line consists an integer N(N≤50) .
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(Q≤100000) . 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
1 12 1 2 3 4 5 6 7 8 9 42 21 22 10 1 2 3 3 4 5 2 3 2 10 10 10 10 11 11 10 1 1 1 2 10 1 11 12 1 10 10 11 11 12
Sample Output
No No No Yes No Yes No No Yes Yes
Source
#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|1
typedef 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)
*/