HDOJ5355 Cake 构造
来源:互联网 发布:ug8.0编程怎么设置速 编辑:程序博客网 时间:2024/05/16 12:48
直接贪心的做法是不对的(例如: 23 6 , 27 7 , 28 7, 31 8 等...)
多校的时候因为SPJ的问题很多WA的程序都过了,实际上这题并没有那么水......
无解的有两种情况 1: sum不能被m整除 2: sum/m比n小
剩下的情况都有解:
标程的做法是根据: n(n+1)/(2*m) 每一组里都有k对组合,如第一行中的(11 100) (12 99) (13 98)....
因为有m行所以这样的组合有2*k*m个,这些对都可以凑成一样的大小
剩下的为粉红色的部分, 这些数字有(n-(2*m-1))%(2*m)+(2*m-1)个 (如样例 1~10),然后对这部分进行从大到小的贪心.....(感觉还是有问题???....)
n=100,m=5的数据
100 5
YES
20 10 1 11 100 12 99 13 98 14 97 15 96 16 95 17 94 18 93 19 92
20 9 2 20 91 21 90 22 89 23 88 24 87 25 86 26 85 27 84 28 83
20 8 3 29 82 30 81 31 80 32 79 33 78 34 77 35 76 36 75 37 74
20 7 4 38 73 39 72 40 71 41 70 42 69 43 68 44 67 45 66 46 65
20 6 5 47 64 48 63 49 62 50 61 51 60 52 59 53 58 54 57 55 56
参考题解: http://blog.csdn.net/queuelovestack/article/details/47321211
Cake
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1347 Accepted Submission(s): 197
Special Judge
Note that you cannot divide a whole cake into small pieces that is each cake must be complete in the
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.
If it is possible, then output
41 25 35 29 3
NOYES1 52 1 42 2 3NOYES3 1 5 93 2 6 73 3 4 8
/* ***********************************************Author :CKbossCreated Time :2015年08月07日 星期五 14时23分36秒File Name :HDOJ5355_2.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;typedef long long int LL;LL n,m;vector<int> vi[10];set<int> v;void init(){for(int i=0;i<10;i++) vi[i].clear();}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int T_T;scanf("%d",&T_T);while(T_T--){cin>>n>>m;init();LL sum=n*(n+1)/2;if(sum%m||n+1<2*m){puts("NO"); continue;}LL c=(n-m*2+1)%(m*2)+m*2-1; v.clear();for(int i=1;i<=c;i++){v.insert(i);}LL s=c*(c+1)/(m*2);for(int i=0;i<m;i++){set<int>::iterator it;LL ts=0;while(ts<s){it=v.upper_bound(s-ts);LL tmp=*--it;vi[i].push_back(tmp);ts+=tmp;v.erase(it);}}int st=c+1;int ed=n;int k=(n-c)/(2*m);for(int i=0;i<m;i++){for(int j=0;j<k;j++){vi[i].push_back(st); st++;vi[i].push_back(ed); ed--;}}puts("YES");for(int i=0;i<m;i++){int sz=vi[i].size();printf("%d",sz);for(int j=0;j<sz;j++){printf(" %d",vi[i][j]);}putchar(10);}} return 0;}
标程的代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;int sol(){ int n,m; scanf("%d%d",&n,&m); if(((n+1ll)*n>>1)%m!=0 || m * 2 -1 > n) return puts("NO"); const int c=(n+1-m*2)%(m*2)+m*2-1, s=c*(c+1)/(m*2), d=(n-c)/(m*2); puts("YES"); set<int> v; for(int i=1;i<=c;++i)v.insert(i); for(int j=0,l=c+1;j<m;++j,putchar('\n')){ static int o[32],c,r; for(c=r=0;r<s;){ auto it = v.upper_bound(s-r); r+=o[c++]=*--it; v.erase(it); } printf("%d",c+d*2); for(int i=0;i<c;++i)printf(" %d",o[i]); for(int i=0;i<d;++i)printf(" %d %d",l++,n--); }}int main(){ int T; for(scanf("%d",&T);T--;sol());}
- HDOJ5355 Cake 构造
- hdu5355--Cake(构造)
- Cake (hdu 5355 构造+dfs)
- hdu 5355 Cake(构造+回溯)
- hdu 5355 Cake dfs暴搜 构造
- hdu 5535 Cake 构造+记忆化搜索
- cake
- Cake
- Cake
- Cake
- Cake
- Cake
- Cake
- Cake
- cake
- Cake
- Cake
- Cake
- leetcode 096 —— Unique Binary Search Trees
- IE下判断IE版本的语句...[if lte IE 6]……[endif]
- poj2752Seek the Name, Seek the Fame
- android menu,activity,AlertDialog... 链接整理
- 淘宝Dubbo框架学习
- HDOJ5355 Cake 构造
- Armstrong树
- JSON解析笔记
- Monkey的学习与使用技巧
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法
- Android out of memory(一)
- Tomcat web.xml Can't convert argument: null错误解决办法
- (六) Multi project setup(多项目设置)
- 引用第三方库So文件时,eclipse会自动把libs下arneabi文件里的so文件删除掉