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

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 (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
  
  
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)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值