题目链接:HDU 5355

http://acm.split.hdu.edu.cn/showproblem.php?pid=5355

Problem Description
There are m soda and today is their birthday. The 1-st soda has prepared n cakes with size 1,2,…,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.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (1≤n≤105,2≤m≤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.

 
Output
For each test case, output "YES" (without the quotes) if it is possible, otherwise output "NO" in the first line.

If it is possible, then output m lines denoting the m parts. The first number si of i-th line is the number of cakes in i-th part. Then si numbers follow denoting the size of cakes in i-th part. If there are multiple solutions, print any of them.

 
Sample Input
4
1 2
5 3
5 2
9 3
 
Sample Output
NO
YES
1 5
2 1 4
2 2 3
NO
YES
3 1 5 9
3 2 6 7
3 3 4 8
 
Author
zimpha@zju
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863 

题意:输入n,m  表示有n个蛋糕,大小为1~n,要均分给m个人,但是每一个蛋糕不能切分,给每个人的蛋糕必须是完整的,输出分配方案;

思路:刚开始,我是用贪心划分的,每个人应该划分的量h=(n+1)*n/2/m  首先给一个人分一个最大的蛋糕x,接下来分h-x,如果h-x已经给别人了,那么继续向下找h-x-1....

这样做是有bug的如23  6  这个数据就会输出NO  ,但实际上是有解的YES

3  11  12  23

4  1    10  13   22

4  2    9    14   21

4  3    8    15   20

4  4    7    16   19

4  5    6    17   18

故这样贪心的做法有bug;

正解:深搜枚举出所有的情况,找到正确的划分方案,这里有一个很好的可以优化,可以发现任取2*m个连续的数都可以均分给m个人,并且n>=2*m-1时才有划分方案,所以令nn=(n-2*m+1)%2*m+2*m-1  从nn+1到n是x*2*m,这部分很好划分,那么只需要对1~nn进行深搜即可;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
int n,m,nn,h;
bool vis[]; set<int>s[];
set<int>::iterator it; int dfs(int cnt,int pos,int sum)
{
if(cnt==m+) return ;
for(int i=pos;i>=;i--)
{
if(!vis[i])
{
vis[i]=true;
if(i+sum<h)
{
s[cnt].insert(i);
if(dfs(cnt,pos-,sum+i)) return true;
s[cnt].erase(i);
}
else if(i+sum==h)
{
s[cnt].insert(i);
if(dfs(cnt+,n,)) return true;
s[cnt].erase(i);
}
vis[i]=false;
}
}
return ;
} int main()
{
int T;
cin>>T;
while(T--)
{
for(int i=;i<;i++)
s[i].clear();
scanf("%d%d",&nn,&m);
if(nn<*m-||(long long)(nn+)*nn/%m!=) {puts("NO"); continue;}
n=(nn-*m+)%(*m)+*m-;
h=(n+)*n//m;
memset(vis,false,sizeof(vis)); dfs(,n,); int go=(nn-*m+)%(*m)+*m-;
int k=(nn-*m+)/(*m);
for(int i=;i<=k;i++)
{
for(int j=;j<=m;j++)
{
s[j].insert(go+j);
s[j].insert(go+*m+-j);
}
go+=*m;
}
puts("YES");
for(int i=;i<=m;i++)
{
printf("%d",s[i].size());
for(it=s[i].begin();it!=s[i].end();it++)
printf(" %d",*it);
puts("");
}
}
}

2015暑假多校联合---Cake(深搜)的更多相关文章

  1. 2015暑假多校联合---Friends(dfs枚举)

    原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...

  2. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  3. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  4. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...

  5. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  6. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  7. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. 每天一个linux命令(29):chgrp命令

    在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...

  2. CSS层模型

    参考:慕课网 点此可进 如何让html元素在网页中精确定位,就像图像软件PhotoShop中的图层一样可以对每个图层能够精确定位操作.CSS定义了一组定位(positioning)属性来支持层布局模型 ...

  3. 理解模板引擎Razor 的原理

    Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.简洁的语法与.NET Framework 结合,广泛应用于ASP.NET MVC 项目.Razor Pad是一 ...

  4. 移动端调试工具DebugGap推荐。

    因为现在项目大部分都是在写移动端,但是调试起来实在是不方便,虽然可以用chrome来模拟手机端,但实际上差别还是有点大的,最近找到了一款比较不错的调试工具,这里分享一下了,虽然网上已经有分享过了,但还 ...

  5. Netbeans不能正常启动glassfish或者部署失败不能运行的问题

    错误信息:D:\临时文件\netbeans\WebTest\build\web中部署GlassFish Server 4, deploy, Connection refused: connect, f ...

  6. poj1273Drainage Ditches

    #include<iostream> /* 题意:就是寻找从源点到汇点的最大流! 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 就是这两个点之间的 ...

  7. Yii2的深入学习--继承关系

    想要了解 Yii2 的话,一定要对 Yii2 中相关类的继承关系有所了解.由于暂时读的代码有限,下面的图中只列出了部分继承关系,之后回跟着源码阅读的越来越多而增加 由上图可以看到 Yii2 中大多数类 ...

  8. H5游戏开发之抓住小恐龙

    第一次写技术性博文,以前都只是写一些生活感想,记录一些生活发生的事情. 博主大三学生一枚,目前学习JS一年多,还处于学习阶段,有什么说的不好的希望大牛指点下,由于第一次写博文,排版什么的有待改进,希望 ...

  9. Android基于mAppWidget实现手绘地图(五)--如何创建地图资源

    地图资源可以通过Slicing Tool工具生成,教程如下: 1.打开Eclipse标准版4.3.2,以Java项目形式导入”slicingtool“项目,运行.(必须是eclipse4.3.2及以上 ...

  10. Oracle数据库常见的误操作恢复方法(上)

    实验环境:Linux6.4 + Oracle 11g 面向读者:Oracle开发维护人员 概要: 1.误操作drop了emp表 2.误操作delete了emp表 3.误操作delete了emp表的部分 ...