转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1517    Accepted Submission(s): 233
Special Judge

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
 

其实这题还是不容易过的,比赛的时候一看居然都过了100人了,而且发现直接贪心是不对的,于是乎乱搞了一发。。。结果居然过了。。。

这题首先只有在不能整除以及n<2m-1的时候才是NO,其余情况都是YES

然后在[2m-1,4m-2]的范围内贪心,然后之后,每2m个相互构一组

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define pb(X) push_back(X)
#define rep(X, N) for(int X=0;X<N;X++)
#define rep2(X, L, R) for(int X=L;X<=R;X++)
typedef long long ll; #define gao() out<<"NO"<<endl
vector<int> ans[];
int used[];
ll tot; class hdu5355 {
public:
int ave; void solve(std::istream &in, std::ostream &out) {
int n, m;
in >> n >> m;
tot = (ll) (n + ) * n / ;
if (tot % m != ) {
gao();
return;
}
int tmp = tot / m;
if (n < * m - ) {
gao();
return;
}
int num = n / m;
if (tmp == n) {
out << "YES" << endl;
out << << " " << n << endl;
rep(i, m - ) {
out << << " " << i + << " " << n - i - << endl;
}
return;
}
if (m == ) {
out << "YES" << endl;
out << n;
rep2(i, , n)out << " " << i;
out << endl;
return;
}
rep2(i, , m)ans[i].clear();
int f = n;
int c = ;
/*while(n>=4*m-1){
rep2(i,1,m){
if(c&1) ans[i].pb(f - i + 1);
else ans[i].pb(f - m + i);
}
f -= m;
n -= m;
c++;
rep2(i,1,m){
if(c&1) ans[i].pb(f - i + 1);
else ans[i].pb(f - m + i);
}
f -= m;
n -= m;
c++;
}*/
c = (n + - m * ) % (m * ) + m * - ;
int d = (n - c) / ( * m);
for (int i = , j = c + ; i <= m; i++) {
rep(k, d)ans[i].pb(j++), ans[i].pb(n--);
}
tot = n;
n = c;
ave = (ll) ( + n) * n / / m;
set<int> s;
rep2(i, , n)s.insert(i);
rep2(i, , m) {
rep(j, ave) {
auto it = s.upper_bound(ave - j);
ans[i].pb(*--it);
j += *it;
s.erase(it);
j--;
}
} // clr(used,0);
// rep(i,tot)a[i] = n - i;
// if(dfs(0,0,n,1)){
//if(dfs(m,n,0,0,n)){
out << "YES" << endl;
// rep2(i,1,n)ans[used[i]].pb(i);
rep2(i, , m) {
int sz = ans[i].size();
out << sz;
rep(j, sz) {
out << " " << ans[i][j];
}
out << endl;
}
//}else out<<"NO"<<endl;
/*if(n % m != 0){
gao();
return;
}
if(num&1){
if(m&1){
out<<"YES"<<endl;
run(m);
rep2(i,1,m){
out<<num;
rep(j,ans[i].size()){
out<<" "<<ans[i][j];
}
int last = 3 * m + i;
int f = 4 * m;
rep2(j,4,num){
out<<" "<<last;
f += m;
if(j&1)last = f- m + i;
else last = f - i + 1;
}
out<<endl;
} }else{
gao();
return;
}
}else{
out<<"YES"<<endl;
rep2(i,1,m){
out<<num;
int last = i;
int f = m;
rep(j,num){
out<<" "<<last;
f += m;
if(j&1)last = f-i+1;
else last = f - m + i;
}
}
}*/ } bool dfs(int num, int now, int u, int m) {
if (now == ) {
int i = tot;
while (used[i])i--;
used[i] = m;
if (dfs(num + , i, i - , m))return ;
used[i] = ;
return ;
}
if (now == ave) {
if (num == tot)return ;
else return dfs(num, , tot, m + );
}
for (int i = u; i > ; i--) {
if (!used[i] && now + i <= ave) {
used[i] = m;
if (dfs(num + , now + i, i - , m))return ;
used[i] = ;
}
}
return false;
} bool dfs(int m, int n, int tot, int num, int now) {
if (!m) {
return ;
}
if (tot = ave) {
if (dfs(m - , n, , num, ))return ;
}
if (tot == ) {
int i = n;
while (used[i])i--;
used[i] = m;
if (dfs(m, n, tot + i, num + , i))return ;
used[i] = ;
return ;
}
for (int i = now; i > ; i--) {
if (!used[i] && tot + i <= ave) {
used[i] = m;
if (dfs(m, n, tot + i, num + , i - ))return ;
used[i] = ;
}
}
return false;
} }; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5355 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = ; i < n; ++i) {
solver.solve(in, out);
} return ;
}

hdu5355 Cake(构造)的更多相关文章

  1. hdu 5535 Cake 构造+记忆化搜索

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给定n与m,其中1<= n <= 1e5,2 <= m <= 10;问 ...

  2. HDU 5355 Cake (构造 + 暴力)

    题意:给定 n,m,让你把 1 ~ n 分成 m 部分,而且每部分和是一样大的. 析:首先先判断不能分成的,第一种是 sum (1 ~ n 的和)不能被 m 整除,或者 sum / m < n, ...

  3. hdu5355 Cake

    Problem Description There are m soda and today is their birthday. The 1-st soda has prepared n cakes ...

  4. HDU 5355 Cake (WA后AC代码,具体解析,构造题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...

  5. codeforces 1282 E. The Cake Is a Lie (dfs+构造)

    链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...

  6. Java的结构之美【1】——构造对象

    当我们遇到多个构造器參数的时候可能会想到用构件器,代码例如以下: /** * 构建器 * @author 阳光小强 * */ public class Lunch { private String c ...

  7. [Cake] 1. CI中的Cake

    在上一篇C#Make自动化构建-简介中,简单的介绍了下Cake的脚本如何编写以及通过Powershell在本地运行Cake脚本.本篇在此基础上,介绍下如何在CI环境中使用Cake. 1. Cake简介 ...

  8. Linux内核分析— —构造一个简单的Linux系统MenuOS(20135213林涵锦)

    Linux内核分析— —构造一个简单的Linux系统MenuOS 实验内容 Linux内核的启动过程,从start_kernel到init进程启动 使用实验楼的虚拟机打开shell cd LinuxK ...

  9. hdu5355 思维+爆搜

    pid=5355">http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m sod ...

随机推荐

  1. android图形基础知识

    Android核心分析(23)-----Andoird GDI之基本原理及其总体框架 2010-06-13 22:49 18223人阅读 评论(18) 收藏 举报 AndroidGDI基本框架 在An ...

  2. 消息机制 - Windows程序设计(SDK)004

    消息机制 让编程改变世界 Change the world by program 内容节选: 我们来回顾一下,窗口是怎么从代码中诞生出来的? 1. 首先我们是通过给 WNDCLASS 窗口类结构各个成 ...

  3. ScrollView嵌套listview 时根据内容动态设置listview高度

    public static void setListViewHeightBasedOnChilds(ListView listView){ ListAdapter listAdapter = list ...

  4. HTTP发送请求模拟

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...

  5. windows下的用户态调试的底层与上层实现

    操作系统:windows XP 调试器通过CreateProcess传入带有DEBUG_PROCESS和DEBUG_ONLY_THIS_PROCESS的dwCreationFlags创建被调试进程.这 ...

  6. HDU 4274 Spy's Work (树 DFS)

    给定N个点,每个点都有一个唯一的前驱结点(点1为大boss),每个点的实际权值是子节点的求和值.现在给出某些点的权值的估算(> , = , < ),问这些估算是否会有冲突,现在保证每个点的 ...

  7. MVC 校验

    校验保障了MVC 应用程序安全性. Models 文件夹包含表示应用程序模型的类 1,创建一个项目MvcValidateDemo. 2,创建一个实体类UserInfo在Models中,包含Id.Use ...

  8. python数据类型—列表(增改删查,统计,取值,排序)

    列表是最常用的数据类型之一,通过列表可以对数据实现方便的存储,修改等操作. 先声明一个空列表: >>> names = [] >>> names [] 可以存多个值 ...

  9. hadoop执行hdfs文件到hbase表插入操作(xjl456852原创)

    本例中需要将hdfs上的文本文件,解析后插入到hbase的表中. 本例用到的hadoop版本2.7.2 hbase版本1.2.2 hbase的表如下: create 'ns2:user', 'info ...

  10. 基于css制作轮播图的部分效果

    在轮播图中,我们可以通过鼠标在特定位置上的滑动来实现元素背景的改变.通常在制作轮播图时,我们首先想到的是js中的交互.可是,如果我们无法使用js,只能单纯的靠css又该如何实现这一效果呢?下面,本人将 ...