题目链接:http://acm.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

题意理解:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之和均相同,如果有解,输出“YES”,并给出每份的蛋糕数及其尺寸大小,否则输出“NO”

开始的时候想的是简单的暴力(肯定没有那么简单),没想到竟然过了,但是之后的spj之后就出现错误了,所以原来的思路还是出现了问题的.

原来的代码:

#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; #define eps 1e-8
#define INF 0x3f3f3f3f
#define LL long long
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
typedef pair<int , int> P;
#define N 100005 bool number[N];//标记蛋糕是否已经取掉
int ans[N];
int n,m; void Find(int x)//查找平均大小为x的分配方案
{
for(int i = 0; i < m; i++)
{
int temp = x,ncount = 0;
for(int j = n;j > 0 && temp != 0 ;j--)
{
if((temp - j >= 0) && number[j] == 0)//每次取能取的最大蛋糕
{
ans[ncount++] = j;
number[j] = 1;
temp -= j;
}
}
printf("%d ",ncount);
for(int j = 0; j < ncount; j++)
printf("%d ",ans[j]);
if(temp>0) cout<<"temp="<<temp<<",该组测试数据不为满足要求...";
printf("\n");
}
} int main()
{
int Case;
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d",&n,&m);
int sum = ((n + 1) * n)>>1;///计算前n个蛋糕的总质量
memset(number,0,sizeof(number));///初始化标记方阵 if(sum % m == 0 && (2 * m - 1) <= n ) ///如果是总质量取余不为0 && 分不到一个
{
printf("YES\n");
Find(sum/m);
}
else
printf("NO\n");
}
return 0;
}

之后借鉴网上的代码,和官网给出的思路很想,但是没有使用递归,感觉这道题的难点有两个,一个是判断什么情况下是直接不行的,但是自己感觉好像不能理解为什么两个条件和结果是充分条件,(现在感觉只是必要条件,有点不懂。。。);另一个是需要分割为两部分,第一部分拼凑,第二部分直接分开输出就好了。

关键是思路。。。

借鉴网址:http://blog.csdn.net/queuelovestack/article/details/47321211

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define ll __int64
using namespace std; int solve()
{
int n,m;
int i,j,k,c,s,d,r,w[32];
set<int> v; set<int>::iterator it;
scanf("%d%d",&n,&m);
if(( (n+1)*n/2)%m || m*2-1>n )
return puts("NO"); c=(n- (m*2-1) )%(m*2)+m*2-1,///算出前一部分有多少个数
s=c*(c+1)/(m*2), ///前面每两个数的和为s
d=(n-c)/(m*2); ///后面每组有多少个 puts("YES");
for(i=1; i<=c; i++)
v.insert(i);
for( j=0,k=c+1; j<m; j++,putchar('\n') )
{
c=r=0;
while(r<s)
{
it=v.upper_bound(s-r);///查找函数,返回查找比查找值大的第一个位置指针(iterator)[使用方法]
--it;
w[c]=*it;///获得该位置指针所指向的数据
r=r+w[c++];
v.erase(it);
}
printf("%d*",c+d*2); ///输出个数 for(i=0;i<c;i++) ///前半部分
printf(" %d",w[i]); for(i=0;i<d;i++)///直接输出后半部分
printf(" %d %d",k++,n--);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
solve();
}

2015 Multi-University Training Contest 6 Cake的更多相关文章

  1. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  2. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  3. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...

  4. 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】

    2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...

  5. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  6. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  7. hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)

    OO's Sequence                                                          Time Limit: 4000/2000 MS (Jav ...

  8. HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  9. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

随机推荐

  1. Android中使用WebView, WebChromeClient和WebViewClient加载网页 (能够执行js)

    Android中使用WebView, WebChromeClient和WebViewClient加载网页   在android应用中,有时要加载一个网页,如果能配上一个进度条就更好了,而android ...

  2. oracle11gRAC环境使用RMAN增量备份方案

    转摘:http://blog.itpub.net/29819001/viewspace-1320977/ [oracle@zx ~]$ rman target /Recovery Manager: R ...

  3. mootools里选择器$,$$,$E,$ES等的区别

    区别就是 $和$$都是1个参数, $适用于ID,或者ID代表的对象 $$适用于CSS选择器 $E和$ES,有2个参数,第二个参数是可选参数代表(filter,即某个ID范围里的元素) $E('inpu ...

  4. SqlServer数据库的查询优化

    建立一个web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是:ADO 纪录集分页法,也就是利用ADO自带的分页功能(利用游标)来实现分页.但这种分页方法仅适用 ...

  5. 06文件与IO

    这节主要学习系统调用stat.lstat.fstat. 格式如下: int fstat(int filedes, struct stat *buf); int stat(const char *pat ...

  6. dns 服务器配置

    1.安装 named 2.配置如下文件: /etc/named.conf // 2 // named.conf 3 // 4 // Provided by Red Hat bind package t ...

  7. C#.NET ,微信退款证书

    微信退款时遇到:基础连接已经关闭 连接被意外关闭. 服务器环境:WIN SERVER 2008 R2.  WINDOWS服务承载的WCF服务,基于.NET FRAMEWORK 3.5. 第一笔交易的退 ...

  8. HackerRank "Training the army" - Max Flow

    First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...

  9. python命令行下tab键补全命令

    在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...

  10. Configure custom SSL certificate for RDP on Windows Server 2012 in Remote Administration mode

    Q: So the release of Windows Server 2012 has removed a lot of the old Remote Desktop related configu ...