hdu5355 Cake
来源:互联网 发布:经济数据公布时间表 编辑:程序博客网 时间:2024/06/14 09:43
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 them parts. Each cake must belong to exact one of m parts.
Note that you cannot divide a whole cake into small pieces that is each cake must be complete in the
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 integersn 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.
The first contains two integers
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 outputm 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.
If it is possible, then output
Sample Input
41 25 35 29 3
Sample Output
NOYES1 52 1 42 2 3NOYES3 1 5 93 2 6 73 3 4 8
这题和木棒拼接正方形很像,用相同的思路就行了。
这里注意dfs可能比较深,所以要手动开栈。#pragma comment(linker, "/STACK:102400000,102400000") 这句话加在程序最前面。
#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;#define maxn 100050#define ll long longint vis[maxn],liang,fen,n;set<int>myset[20];set<int>::iterator it;int dfs(int x,int pos,ll len){int i;if(x==fen)return 1;for(i=pos;i>=1;i--){if(!vis[i]){vis[i]=1;if(len+i<liang){myset[x].insert(i);if(dfs(x,i-1,len+i))return 1;myset[x].erase(i);}else if(len+i==liang){myset[x].insert(i);if(dfs(x+1,n,0))return 1;myset[x].insert(i);}vis[i]=0;}}return 0;}int main(){int i,j,T;ll num;scanf("%d",&T);while(T--){scanf("%d%d",&n,&fen);num=(ll)(n+1)*n/2;if(n<fen || num%fen!=0 || num/fen<n){printf("NO\n");continue;}liang=num/fen;memset(vis,0,sizeof(vis));for(i=0;i<=fen;i++){myset[i].clear();}if(dfs(0,n,0)){printf("YES\n");for(i=0;i<fen;i++){printf("%d",myset[i].size());for(it=myset[i].begin();it!=myset[i].end();it++){printf(" %d",*it);}printf("\n");}}else printf("NO\n");}return 0;}/*10050 10NO40 10YES3 3 39 403 7 37 383 11 35 363 15 33 343 19 31 323 23 29 304 1 26 27 285 2 9 22 24 255 6 17 18 20 218 4 5 8 10 12 13 14 16*/
0 0
- hdu5355 Cake
- hdu5355--Cake(构造)
- hdu5355
- [贪心]hdu5355 多校联合第六场 Cake
- [搜索]hdu5355
- cake
- Cake
- Cake
- Cake
- Cake
- Cake
- Cake
- Cake
- cake
- Cake
- Cake
- Cake
- Cake
- 安卓手机的APP图标尺寸规范和图标命名规范
- 解决Unsupported major.minor version 51.0错误
- hadoop配置
- 两道笔试编程题(求水仙数和求平方根数组之和)
- 在myeclipse中写mysql语句出现的一个报错
- hdu5355 Cake
- HDOJ 1201 18岁生日
- hdu 2602 - Bone Collector(01背包)解题报告
- Linked List Cycle
- 粒子滤波(PF:Particle Filter)
- poj 2993 Emag eht htiw Em Pleh
- LeetCode之Same Tree
- 史上最全的CSS hack方式一览
- c++中的三种排序,比较基础,因为作为一个编程员总要会几种排序方法