HDU 5355 Cake (构造 + 暴力)
题意:给定 n,m,让你把 1 ~ n 分成 m 部分,而且每部分和是一样大的。
析:首先先判断不能分成的,第一种是 sum (1 ~ n 的和)不能被 m 整除,或者 sum / m < n,其他的情况都有解。
这个题采用的是构造加暴力搜索的思想,首先,先成对的构造解,也就 2 * m 个,每 2 * m 组,分别放到 m 个部分,这样都每部分的贡献都是一样的(最大的和最小一组,次大和次小等等),然后剩下的部分进行搜索暴力,但是要注意的是,如果剩下的不够 m 个的话,这样是不可能搜索出解的,要再加上一个 2*m,但是加上一个 2 * m,复杂度就大了很多,会超时,所以可以对于大于 20 的再减去 m,这样就能够少搜索一部分。就是有一组神数据,那就是 20 6 (这个是我用暴力找出来的),如果特判这个数据的话 嘻嘻 就更容易了。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #include <sstream>
- #include <list>
- #include <assert.h>
- #include <bitset>
- #include <numeric>
- #define debug() puts("++++")
- #define gcd(a, b) __gcd(a, b)
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define fi first
- #define se second
- #define pb push_back
- #define sqr(x) ((x)*(x))
- #define ms(a,b) memset(a, b, sizeof a)
- #define sz size()
- #define be begin()
- #define ed end()
- #define pu push_up
- #define pd push_down
- #define cl clear()
- #define lowbit(x) -x&x
- //#define all 1,n,1
- #define FOR(i,n,x) for(int i = (x); i < (n); ++i)
- #define freopenr freopen("in.in", "r", stdin)
- #define freopenw freopen("out.out", "w", stdout)
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const LL LNF = 1e17;
- const double inf = 1e20;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 1e5 + 20;
- const int maxm = 1e6 + 10;
- const LL mod = 1000000000000000LL;
- const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
- const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
- const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline bool is_in(int r, int c) {
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- inline int readInt(){ int x; scanf("%d", &x); return x; }
- int a[50][50];
- int b[50];
- bool dfs(int cur){
- if(cur == 0) return true;
- for(int i = 0; i < m; ++i) if(a[i][40] + cur <= b[i]){
- a[i][40] += cur;
- a[i][++a[i][0]] = cur;
- if(dfs(cur-1)) return true;
- a[i][40] -= cur;
- --a[i][0];
- }
- return false;
- }
- int main(){
- int T; cin >> T;
- while(T--){
- scanf("%d %d", &n, &m);
- vector<int> ans[20];
- LL sum = (LL)n * (n+1) / 2;
- LL p = sum / m;
- if(sum % m || sum < (LL)n * m){ puts("NO"); continue; }
- puts("YES");
- int t = n % (m<<1);
- if(t) t += m<<1;
- t = min(t, n);
- for(int i = t+1; i < n; i += (m<<1)){
- for(int j = 0; j < m; ++j) ans[j].pb(i+j);
- for(int j = 0; j < m; ++j) ans[j].pb(i+(m<<1)-j-1);
- }
- sum = t * (t+1) / 2 / m;
- for(int i = 0; i < m; ++i) b[i] = sum;
- if(t > 20){
- for(int i = t, j = 0; j < m; ++j, --i) b[j] -= i, ans[j].pb(i);
- t -= m;
- }
- ms(a, 0);
- dfs(t);
- for(int i = 0; i < m; ++i){
- printf("%d", ans[i].sz + a[i][0]);
- for(int j = 0; j < ans[i].sz; ++j) printf(" %d", ans[i][j]);
- for(int j = 1; j <= a[i][0]; ++j) printf(" %d", a[i][j]);
- printf("\n");
- }
- }
- return 0;
- }
HDU 5355 Cake (构造 + 暴力)的更多相关文章
- HDU 5355 Cake
HDU 5355 Cake 更新后的代码: 今天又一次做这道题的时候想了非常多种思路 最后最终想出了自觉得完美的思路,结果却超时 真的是感觉自己没救了 最后加了记忆化搜索,AC了 好了先说下思路吧.不 ...
- 多校第六场 1003 hdu 5355 Cake(贪心)
题目链接:(数据加强后wa了) hdu 5355 题目大意: 给出一个蛋糕.切成1~n大小的n块.问是否能在不继续分割的情况下拼凑出m等份. 题目分析: 首先我们是可以知道每份蛋糕的尺寸的,利用n*( ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
- 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...
- hdu 5535 Cake 构造+记忆化搜索
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给定n与m,其中1<= n <= 1e5,2 <= m <= 10;问 ...
- HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)
Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Sub ...
- 贪心 HDOJ 5355 Cake
好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...
- 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination
题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...
- hdoj 5355 Cake(分析+二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 分蛋糕的题目,有1-n大小的n个蛋糕,要求平均分成m份,不能切开蛋糕 #include<s ...
随机推荐
- Serializer fields
1.导入 from rest_framework import serializers field参数 1.read_only read_only=True 表示不允许用户自己上传,只能用于api的输 ...
- Excel下拉选项二级联动
在日常工作中,难免遇到操作excel的时候,二级联动下拉选项多用于像地市区县的应用场景 1)先把要联动的内容准备好,把它放到第二个sheet页中 2)操作级联的文本 全部选中之后,Ctrl+G -- ...
- 视频修复工具recover_mp4,视频录制一半掉电,如何查看已保存数据?
在生产环境中,视频通常是一种重要的文件证据,但是,如果因为各种原因,导致视频在录制到一半过程中失败, 比如:监控到一半,录制设备掉电.虽然,掉电后的视频肯定找不到,但是,有时,长时间工作生产的视频通常 ...
- HttpServletRequest 获取cookie
request.getHeader("cookie") 得到的是a=b,c=d Cookie[] cookies = request.getCookies(); 访问方在heade ...
- oracl遇到的问题
使用oracl数据库用 ALTER TABLE Students ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (sname) 添加唯一性约束,出现问题,报错为:a ...
- 使用Dockerfile自定义一个包含centos,tomcat的镜像
1.首先建立一个专用的dockerfile目录,方便统一存放将要创建的Dockerfile文件及相关资源, 例如:mkdir mydockerself 2.定位到mydockerself路径下,下载l ...
- Python学习—框架篇之初识Django
什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的W ...
- Windows Server 2012 R2 双网卡绑定
双网卡绑定主要有以下两点好处: 1.实现网络容错:主主模式和主被模式 2.带宽聚合 首先准备工作需要两台虚拟机,Server01是目标服务器,需要有两块网卡,并且清空两块网卡的现有配置,Server0 ...
- MyCP -tx -xt 功能的Java实现
MyCP -tx -xt 功能的Java实现 功能简介 java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十进制数字)转化为二进制文件 java MyCP -xt ...
- SpringMVC的执行流程
解析SpringMVC执行流程 SpringMVC的执行图大概如下所示 1.首先它是由客户端发起请求,到DispatcherServlet,被拦截后到HandlerMapping这里充当一个路由器,通 ...