题意:给指定数量的数字“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. Static File Middleware

    [ASP.NET Core] Static File Middleware   前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...

  2. CSS随手记

    html5模板 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  3. python 下载安装及setuptools应用

    1.首先下载python安装程序,下载地址:https://www.python.org/download/releases/2.7.8/ 如下图: 因为我的机器是32位的就选择了Windows x8 ...

  4. Automotive Security的一些资料和心得(5):Privacy

    1. Introduction 1.1 "Customers own their data and we can be no more than the trsted stewards of ...

  5. 回首Java(始)

    自接触Java开始,已然5载春秋. 如今每每在深入学习过程中,时刻感到力不从心. It's time!该拾起J2SE基石. 平地高楼,日积跬步.根底不坚实,如今才寸步维艰. 回头再温故.

  6. javascript移动设备触屏事件

    ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件: /** * onTouchEvent */ var div = d ...

  7. [cc150] 括号问题

    Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of p ...

  8. hdu 4286

    splay 练手用: 杭电的oj要手动开栈: #include<cstdio> #pragma comment(linker, "/STACK:102400000,1024000 ...

  9. QReadWriteLock 读写锁的区别

    QReadWriteLock 这个允许多个进程同时读,但是只有一个写.而且写读不能同时进行. 文档里语焉不详,这是我自己的理解: lockForWrite 为写而锁,就是要修改数据,外人连想进来读数据 ...

  10. 如何迁移SharePoint 2010至新的站点

    SharePoint使用非常方便,但是有一个问题获取会困扰大家,就是SharePoint的备份和迁移.下面我们来看一下如何把SharePoint迁移至别的SharePoint站点. 1. 使用网站集管 ...