题意:给指定数量的数字“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. Sqlmap下载安装与基础命令使用

    本文介绍一下Sqlmap的安装跟配置环境变量. 顺便附上一些常用的命令 SQLMAP-64位.Python 下载链接:http://pan.baidu.com/s/1c0D82fm 密码:d7ec P ...

  2. org.springframework.orm.jpa.JpaTransactionManager

    [第九章] Spring的事务 之 9.2 事务管理器 ——跟我学spring3 http://sishuok@com/forum/blogPost/list/0/2503.html

  3. ios短信和电话--参考

    调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调 ...

  4. css清除浮动的几种方法整理

    四种清除浮动方法如下: 1.使用空标签清除浮动.空标签可以是div标签,也可以是P 标签.这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签 清除浮动,并为其定义CSS代码:cle ...

  5. [codility]tape_equilibrium

    http://codility.com/demo/take-sample-test/tapeequilibrium 简单题.记录到i为止的sum就可以了.O(n). // you can also u ...

  6. Tomcat部署(转)

    首先说说tomcat的几种部署方法: 1.将应用文件夹或war文件塞到tomcat安装目录下的webapps子目录下,这样tomcat启动的时候会将webapps目录下的文件夹或war内容当成应用部署 ...

  7. C语言动态内存管理

    1-概述 动态存储管理的基本问题是:系统如何按请求分配内存,如何回收内存再利用.提出请求的用户可能是系统的一个作业,也可能是程序中的一个变量. 空闲块 未曾分配的地址连续的内存区称为“空闲块”. 占用 ...

  8. 【HDOJ】5046 Airport

    DLX简单题目. /* 5046 */ #include <iostream> #include <string> #include <map> #include ...

  9. Android开发UI之自定义控件的皮肤

    定义一个button的皮肤,设置属性android:background="@drawable/button_skin",button_skin.xml文件为要下文中的资源文件. ...

  10. 获得设备型号(含iPhone6 , iPhone 6+)

    //获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { int mib[2]; size_t len ...