HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)
Cake
Time Limit: 2000/1000 MS (Java/Others) Memory
Limit: 131072/131072 K (Java/Others)
Total Submission(s): 965 Accepted Submission(s): 119
Special Judge
and today is their birthday. The 1-st
soda has prepared n cakes
with size 1, 2, \dots, n.
Now 1-st
soda wants to divide the cakes into m parts
so that the total size of each part is equal.
Note that you cannot divide a whole cake into small pieces that is each cake must be complete in the m parts.
Each cake must belong to exact one of m parts.
indicating the number of test cases. For each test case:
The first contains two integers n and m (1
\le n \le 10^5, 2 \le m \le 10),
the number of cakes and the number of soda.
It is guaranteed that the total number of soda in the input doesn’t exceed 1000000. The number of test cases in the input doesn’t exceed 1000.
If it is possible, then output m lines
denoting the m parts.
The first number s_i of i-th
line is the number of cakes in i-th
part. Then s_inumbers
follow denoting the size of cakes in i-th
part. If there are multiple solutions, print any of them.
4
1 2
5 3
5 2
9 3
NO
YES
1 5
2 1 4
2 2 3
NO
YES
3 1 5 9
3 2 6 7
3 3 4 8
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#define LL long long
using namespace std;
const LL MAXN = 500000 + 10;
LL n, m;
LL ans[20][MAXN];
LL A[MAXN];
LL pos[MAXN];
LL tot, tar;
bool dfs(LL dep, LL now, LL u, LL c)
{
if(now == 0)
{
LL k = 0;
while(pos[k] != -1) k++;
pos[k] = c;
if(dfs(dep + 1, A[k], k + 1, c)) return true;
pos[k] = -1;
return false;
}
if(now == tar)
{
if(dep == tot) return true;
if(dfs(dep, 0, 0, c + 1)) return true;
}
for(LL i=u;i<tot;i++)
{
if(pos[i] == -1 && now + A[i] <= tar)
{
pos[i] = c;
if(dfs(dep + 1, now + A[i], i + 1, c)) return true;
pos[i] = -1;
}
}
return false;
}
int main()
{
LL T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(LL i=0;i<m;i++) for(LL j=0;j<n;j++) ans[i][j] = 0;
if((n * (n + 1) / 2) % m != 0 || (2 * m - 1) > n)
{
printf("NO\n");
continue;
}
while(n >= 40)
{
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - i;
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - 2 * m + 1 + i;
n -= 2 * m;
}
tot = n;
tar = n * (n + 1) / (2 * m);
for(LL i=0;i<tot;i++) A[i] = tot - i;
for(LL i=0;i<tot;i++) pos[i] = -1;
dfs(0, 0, 0, 0);
for(LL i=0;i<tot;i++) ans[pos[i]][++ans[pos[i]][0]] = A[i];
printf("YES\n");
for(LL i=0;i<m;i++)
{
printf("%d", ans[i][0]);
for(LL j=1;j<=ans[i][0];j++) printf(" %d", ans[i][j]);
printf("\n");
}
}
return 0;
}
HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)的更多相关文章
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- HDU 5355 Cake
HDU 5355 Cake 更新后的代码: 今天又一次做这道题的时候想了非常多种思路 最后最终想出了自觉得完美的思路,结果却超时 真的是感觉自己没救了 最后加了记忆化搜索,AC了 好了先说下思路吧.不 ...
- 多校第六场 1003 hdu 5355 Cake(贪心)
题目链接:(数据加强后wa了) hdu 5355 题目大意: 给出一个蛋糕.切成1~n大小的n块.问是否能在不继续分割的情况下拼凑出m等份. 题目分析: 首先我们是可以知道每份蛋糕的尺寸的,利用n*( ...
- 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5289 Assignment(2015 多校第一场二分 + RMQ)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j ...
- hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...
- hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语
题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...
随机推荐
- jQuery的deferred对象详解(转)
jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本. 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5.0版本开始引入的一个新功能----deferred对象. ...
- Scala 将BigDecimal转换为Long
待转换.asInstanceOf[Number].longValue (Double转为Long也适用)
- Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)
转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...
- cocos2d-之音乐背景播放
1.先加入头文件 #include <SimpleAudioEngine.h> using namespace CocosDenshion;//为了方便以下的函数使用,使用命名空间 2.在 ...
- Spine U3D整合流程问题
Spine U3D整合流程问题 What: 公司2d项目开发,动画外包的spine.本来在spine里面一切正常,但是导入u3d运行库的时候动画切换的时候原来的动画是好的,一旦切换了就乱帧了. 如下结 ...
- Scapy基础学习之中的一个
关于Scapy Scapy的是一个强大的交互式数据包处理程序(使用python编写). 它可以伪造或者解码大量的网络协议数据包,可以发送.捕捉.匹配请求和回复包等等.它可以非常easy地处理一些典型操 ...
- c和c++在windows下获取时间和计算时间差的方法总结
c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...
- 解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
This resolved the issue for me. Your code becomes: public Excel.Application excelApp = new Excel.App ...
- Andrew Ng机器学习课程6
Andrew Ng机器学习课程6 说明 在前面尾随者台大机器学习基石课程和机器学习技法课程的设置,对机器学习所涉及到的大部分的知识有了一个较为全面的了解,可是对于没有动手敲代码并加以使用的情况,基本上 ...
- OpenERP7.0 忘记admin管理员密码解决办法
操作环境:linux Ubuntu12.10 OpenERP7.0 以下操作,请注意大小写 切换用户 root@ubuntu:~# su - postgres 查看数据库 postgres@ubun ...