HDU 5355 Cake

题目链接:https://cn.vjudge.net/contest/226118#problem/C

转载:https://blog.csdn.net/queuelovestack/article/details/47321211
          https://blog.csdn.net/helloworld10086/article/details/47337379

Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Special Judge

Problem Description
There are  soda and today is their birthday. The -st soda has prepared  cakes with size . Now -st soda wants to divide the cakes into  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  parts. Each cake must belong to exact one of  parts.
 

Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:

The first contains two integers  and  , 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  lines denoting the  parts. The first number  of -th line is the number of cakes in -th part. Then  numbers follow denoting the size of cakes in -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”

例如n=5,m=3,即大小尺寸分别为1,2,3,4,5的5个蛋糕,要求分成三份,那么解可以是第一份一个蛋糕,大小为5;第二份两个蛋糕,大小为1、4;第三份两个蛋糕,大小为2、3。这样每份大小之和均为5,满足题目要求。

放上出题人的解题报告


解题思路:此题无解的情况无外乎两种:①所有蛋糕的大小之和无法被m整除;②每一份蛋糕的大小之和不得小于蛋糕大小的最大值n,即sum/m>=n,转化一下为n*m<=sum,亦可以将sum代入,转化为n>=2*m-1。

之后便是有解的情况,有解的情况每份蛋糕的大小之和是知道的,即sum/m。然后我们可以知道n最小得是2*m-1

譬如n=5,m=3,

5 3

1 5

2 4 1

2 3 2

实我们需要做的就是把分配方式分成两部分,一部分是k个2*m,另外就是剩下的那部分,我们来看看n=23,m=6这组数据,之前AC的代码普遍过不了这组数据

23 6
YES
11      12  23
10  1  13  22
4   9  2  14  21
4   8  3  15  20
4   7  4  16  19
4   6  5  17  18

先利用(n-m*2+1)%(m*2)+m*2-1算出粉色那部分数据的大小,然后利用贪心的思想二分一下即可找到符合的蛋糕大小,剩下的绿色部分则是k个2*m的部分,该部分小的数是正向递增的,大的数是反向递减的,所以必定是符合要求的。为了更加直观,放上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

做法一:(纯构造)

#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;  
const int N = 100005;  
const int inf = 1000000000;  
const int mod = 1000000007;  
int solve()  
{  
    __int64 n,m;  
    int i,j,k,c,s,d,r,w[32];  
    set<int> v;  
    set<int>::iterator it;  
    scanf("%I64d%I64d",&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,//蛋糕的分配方式可以分成两部分,前一部分至少有2*m-1个,  
    s=c*(c+1)/(m*2),        //而后面必须要有k个2*m,那多出来的部分就归到前一部分去  
    d=(n-c)/(m*2);          //即(n-m*2+1)%(m*2)+m*2-1  
    puts("YES");  
    for(i=1; i<=c; i++)  
        v.insert(i);  
    for(j=0,k=c+1;j<m;j++,putchar('\n'))  
    {  
        for(c=r=0;r<s;)//s为每份蛋糕前一部分的和  
        {  
            it=v.upper_bound(s-r);//通过二分找使总和最接近s的蛋糕大小  
            r+=w[c++]=*--it;  
            v.erase(it);  
        }  
        printf("%d",c+d*2);//c+d*2表示每组蛋糕的个数,d即是题解中提到的k个2*m的k  
        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;  
    for(scanf("%d",&t);t--;solve());  
}

做法二:(dfs+构造)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
ll n,m,res,dis;
vector <int> ans[maxn];
int vis[maxn],cake[maxn];
int dfs(int cur,int sum)
{
    if(cur==m+1) return true;

    for(int i=res;i>=1;i--)
    {
        if(vis[i]) continue;

        if(sum+i==dis)
        {
            cake[i]=cur;
            vis[i]=1;
            if(dfs(cur+1,0))
            {
                return 1;
            }
            vis[i]=0;
            return 0;
        }
        else if(sum+i<dis)
        {
            cake[i]=cur;
            vis[i]=1;
            if(dfs(cur,sum+i))
            {
                return 1;
            }
            vis[i]=0;
        }
    }
    return 0;
}
int main()
{
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%lld%lld",&n,&m);
        for(int i=0;i<=m;i++)
        {
            ans[i].clear();
        }
        ll sum=n*(n+1)/2;
        ll ave=sum/m;
        if(sum%m)
        {
            printf("NO\n");
            continue;
        }
        if(ave<n)
        {
            printf("NO\n");
            continue;
        }

        res=n%(2*m);
        if(res!=0)
        {
            res+=2*m;
            res=min(res,n);
        }

        ll temp=0;
        int cur=res+1;
        for(int i=1; i<=m; i++)
        {
            for(int cur=res+1,j=0; j<(n-res)/(2*m); j++)
            {
                ans[i].push_back(cur+((i-1)*(n-res)/(2*m)+j));
                ans[i].push_back(n-((i-1)*(n-res)/(2*m)+j));
                if(i==1)
                {
                    temp+=(cur+j);
                    temp+=(n-j);
                }
            }
        }


        dis=ave-temp;

        dfs(1,0);


        printf("YES\n");
        for(int i=1;i<=res;i++)
        {
            ans[cake[i]].push_back(i);
        }

        for(int i=1;i<=m;i++)
        {
            int len=ans[i].size();
            printf("%d",len);
            for(int j=0;j<len;j++)
            {
                printf(" %d",ans[i][j]);
            }
            printf("\n");
        }

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值