HDOJ5355 Cake 构造


直接贪心的做法是不对的(例如: 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


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   (1n105,2m10) , 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
 

Source
 

ac的代码:

/* ***********************************************
Author        :CKboss
Created 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());
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值