题意:给指定数量的数字“1”,“2”,“3”……,“9”。用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小。

能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11的倍数。

所以想到把所有数字分成两部分,即奇数位部分和偶数位部分,两部分的差相0即能被11整除(MOD 11)。

所求可以化为,其中一部分%11的余数为所有数字之和%11的余数的一半。

dp[k][r] := 能否找到 任意k个数之和 %11 == R

#include<bits/stdc++.h>
using namespace std; int dig[12];
bool dp[105][12]; int main()
{
int t;
scanf("%d", &t);
while (t--) {
int n = 0; //数字个数
int sum = 0; //所有数字之和
memset(dp, 0, sizeof dp);
for (int i = 1; i <= 9; ++i) {
scanf("%d", dig + i);
n += dig[i];
sum += dig[i] * i;
}
int m = sum % 11;
if (m & 1) m += 11;
m /= 2;
dp[0][0] = true; for (int i = 1; i <= 9; ++i) {
for (int j = 0; j < dig[i]; ++j) {
for (int k = n; k >= 1; --k) {
//for (int k = 1; k <= n; ++k) {
for (int r = 0; r < 11; ++r) {
dp[k][r] |= dp[k - 1][(r - i + 11) % 11];
}
}
}
}
int flag = 1;
for (int i = n / 2; i >= 0; --i) {
if (dp[i][m]) {
printf("%d\n", n + max(n - i - 1 - i, 0));
flag = 0;
break;
}
}
if (flag) {
printf("-1\n");
}
}
return 0;
}

  

UVALive 5111 Soccer Teams (动态规划)的更多相关文章

  1. Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划

    Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...

  2. UVALive 6486 Skyscrapers 简单动态规划

    题意: 有N个格子排成一排,在每个格子里填上1到N的数(每个只能填一次),分别代表每个格子的高度.现在给你两个数left和right,分别表示从左往右看,和从右往左看,能看到的格子数.问有多少种情况. ...

  3. PHP的SPL标准库里面的堆(SplHeap)怎么使用

    PHP的SPL标准库里面的堆(SplHeap)怎么使用 一.总结 1.因为SplHeap是抽象类,所以要先继承,实现里面的抽象方法compare后,才能new对象使用. 二.PHP的SPL标准库里面的 ...

  4. KDD2015,Accepted Papers

    Accepted Papers by Session Research Session RT01: Social and Graphs 1Tuesday 10:20 am–12:00 pm | Lev ...

  5. POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)

    POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...

  6. 【CF1133E】K Balanced Teams(动态规划,单调队列)

    [CF1133E]K Balanced Teams(动态规划,单调队列) 题面 CF 让你把一堆数选一些出来分成不超过\(K\)组,每一组里面的最大值和最小值之差不超过\(5\),求最多有多少个人元素 ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

  9. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

随机推荐

  1. 蓝桥杯——FJ字符串

    FJ在沙盘上写了这样一些字符串: A1 = "A"1 A2 = "ABA"3 A3 = "ABACABA"7 A4 = "ABAC ...

  2. App新版本提醒

    童鞋们是否遇过在使用app的时候出现有新版本更新跳转到苹果商店的提示呢? github中有一个三方框架 Harpy 完美实现了这一功能! demo时刻 我的github - Harpy

  3. sjtu1585 oil

    Description Crystal家的公司最近承包了一个大油田.整块油田为一个矩形区域,被划分为\(n \times m\)个小块. Crystal亲自调查了每个小块的石油储备量.这些数据表示为\ ...

  4. python登录豆瓣,发帖

    学习了urllib.urllib2及cookielib常用方法的使用登录豆瓣,由于有验证码,采取的办法是将验证码图片下载到同目录下,查看图片后输入验证码即可登录.发帖帖子内容写死在代码中了 [Pyth ...

  5. ZOJ 3609 Modular Inverse

    点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...

  6. 利用动软代码生成器 自动生成LINQ需要用的数据实体类 (转)

    首先先建立一个模板 名称随意 我起的“生成数据实体.cmt” 代码如下: <#@ template language="c#" HostSpecific="True ...

  7. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  8. Joda-Time

    任何企业应用程序都需要处理时间问题.应用程序需要知道当前的时间点和下一个时间点,有时它们还必须计算这两个时间点之间的路径.使用 JDK 完成这项任务将非常痛苦和繁琐.现在来看看 Joda Time,一 ...

  9. spring junit class path resource [ /com/config/spring-core.xml] cannot be opened because it does not exist

    正确写法应该如下: @RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(locations="classpath: ...

  10. Hadoop家族学习路线图

    主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, Chukwa,新增加的项 ...