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次运算的过程,如果不能输 ...
随机推荐
- 如何让Ubuntu 14重启后,保存屏幕亮度的设置
每次重新启动后,系统的屏幕亮度都会被调节到最大值,挺烦人的!如何让它保存设置值呢? 首先,我们必须知道自己系统的屏幕亮度取值范围,打开终端,按照下面的步骤进行: 进入backlight这个目录 cd ...
- 搭建redis集群环境
Redis的集群机制 ============================= 转自http://lib.csdn.net/article/redis/39999 别人写的,写得不错,转了. Red ...
- 《Javascript高级程序设计》的一些可疑点
1.null和undefined的区别(出自第三章基本概念的3.4数据类型) 2.sort-比较函数(5.2.5重排序方法)
- iOS:GitHub上值得关注的iOS开源项目
1.AFNetworking地址:https://github.com/AFNetworking/AFNetworking用于网络请求 2.JSONKit地址:https://github.com/j ...
- 【Todo】Apache-Commons-Pool及对象池学习
有这篇文章: http://www.cnblogs.com/tommyli/p/3510095.html 方案提供了三种类型的pool,分别是GenericKeyedObjectPool,SoftRe ...
- js/jquery循环提取table单元格值
<table id="tbitem"> <tr><td>测试数据1</td></tr> <tr><td ...
- Druid对比Hadoop
Druid对比Hadoop Hadoop 向世界证明, 花费很少的钱实现典型的解决方案, 将数据保存在一般的商用机器的数据仓库里是可行的. 当人们将自己的数据保存在Hadoop, 他们发现两个问题 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-TwinCAT自带的找原点功能块MC_Home怎么用
对于相对编码器类型轴(包括虚拟轴),可以使用贝福提供的找原点功能块MC_Home. HomingMode是指机器在往前跑的时候(30单位/s的默认速度),当碰到阻挡,则会有一个布尔值从FALSE改 ...
- Java 多线程之 synchronized 和 volatile 的比較
概述 在做多线程并发处理时,常常须要对资源进行可见性訪问和相互排斥同步操作.有时候,我们可能从前辈那里得知我们须要对资源进行 volatile 或是 synchronized 关键字修饰处理.但是,我 ...
- mybatis 自动更新表结构 ,兼容通用tkmapper
1.maven引入jar <dependency> <groupId>com.github.gonglb.tools</groupId> <artifactI ...