2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355
http://acm.split.hdu.edu.cn/showproblem.php?pid=5355
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.
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.
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.
题意:输入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(深搜)的更多相关文章
- 2015暑假多校联合---Friends(dfs枚举)
原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 2015暑假多校联合---CRB and His Birthday(01背包)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...
- 2015暑假多校联合---Expression(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...
- 2015暑假多校联合---Zero Escape(变化的01背包)
题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- 2015暑假多校联合---Problem Killer(暴力)
原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
随机推荐
- png图片尺寸大小调整
Android 开发中经常遇到各种hdpi,xhdpi,xxhdpi....很多尺寸大小的png图片要求. 网上也有不少工具,今天我又发现一款在线转换大小的网站,用了一下,一个png 114X114 ...
- Atitit 桌面软件跨平台gui解决方案 javafx webview
Atitit 桌面软件跨平台gui解决方案 javafx webview 1.1. 双向js交互1 1.2. 新弹出窗口解决1 1.3. 3.文档对象入口dom解析1 1.4. 所以果断JavaFX, ...
- iOS-数据解析XML解析的多种平台介绍
在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML.TouchXML.KissXML.Tiny ...
- cmder设置打开时的默认目录
cmder设置打开时的默认目录 打开cmder自动进入工作目录,怎么配置? http://superuser.com/questions/1005285/run-a-bat-file-with-cmd ...
- pycharm运行脚本为何不生成测试报告?
今日使用python+selenium编写自动化测试脚本并执行过程中,使用pycharm运行结果后发现脚本运行无报错,脚本中的操作也正常被执行,但就是没有生成测试报告. 为什么呢,为什么呢,生成测试报 ...
- Oracle 11g系列:视图
视图是数据库中特有的对象,视图用于存储查询,但不会存储数据(物化视图除外).这是视图和数据表的重要区别.Oracle中有4种视图:关系视图.内嵌视图.对象视图和物化视图. 1.关系视图 1>.创 ...
- 深入理解javascript函数系列第一篇——函数概述
× 目录 [1]定义 [2]返回值 [3]调用 前面的话 函数对任何一门语言来说都是一个核心的概念.通过函数可以封装任意多条语句,而且可以在任何地方.任何时候调用执行.在javascript里,函数即 ...
- 移动web开发之视口viewport
× 目录 [1]布局视口 [2]视觉视口 [3]理想视口[4]meta标签[5]总结 前面的话 在CSS标准文档中,视口viewport被称为初始包含块.这个初始包含块是所有CSS百分比宽度推算的根源 ...
- UVAoj 348 - Optimal Array Multiplication Sequence
/* 题意:矩阵相乘的最少的步数 dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j]+num[i-1]*num[k]*num[j]); 表示的是第i个矩阵到第j个矩阵 ...
- 编写可编辑的List控件
今天由于项目的需要,要编写个能编辑的List控件,即双击List控件的表项能修改其中的内容.由于MFC自带的List控件是不能编辑的,所以在网上找了下怎样编写可编辑的CListCtrl控件,基本上都是 ...