HDU 5355 Cake(原先錯誤的代碼已更新),hdu5355
Cake
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Special Judge
Problem DescriptionThere 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.
InputThere 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.
OutputFor 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
41 25 35 29 3
Sample Output
NOYES1 52 1 42 2 3NOYES3 1 5 93 2 6 73 3 4 8
Source2015 Multi-University Training Contest 6 /*********************************************************************************/
該題有點坑,比賽的時候spj出了問題,結果成了一道AC率高達50%的水題,比賽結束後更新了spj,使得原先AC的代碼大部分都WA了,現在我來講講能AC的一種方法,之前說晚些會放上另一種方法,但是不知道哪裡出了問題,一直TLE,所以不能與大家分享了,對不起。
題意:給你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
2*m-1就說明了n本身可以自己一組,其他兩兩一組,一個正向遞增,一個反向遞減,必定可以湊成n,當然這是奇數的情況
但該方法其實是不分奇偶性的,上述只是說明。其實我們需要做的就是把分配方式分成兩部分,一部分是k個2*m,另外就是剩下的那部分,我們來看看n=23,m=6這組資料,之前AC的代碼普遍過不了這組資料
23 6
YES
3 11 12 23
4 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
上AC代碼,如果仍有不理解的可以在此文章下評論,我會儘快予以回答,謝謝
#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 __int64using 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());}
//此題資料被更新,上述方法是已經AC的代碼,但以下方法已經不能AC,留此作為反例借鑒
題意:給你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/m,那麼我們只需將蛋糕從大到小暴力枚舉填滿每份的大小之和即可。例如n=5,m=3,即大小尺寸分別為1,2,3,4,5的5個蛋糕,要求分成三份,那麼第一份我先取最大的蛋糕,即5,已滿足第一份蛋糕要求的大小;第二份蛋糕同樣先取未取過的最大的蛋糕,然後依次放入相加之和不會超過平均值的蛋糕,即先放進4,再放進1;第三份亦如此。這樣每份大小之和依然均為5,滿足題目要求。
詳細見代碼
#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 __int64using namespace std;const int N = 100005;const int inf = 1000000000;const int mod = 1000000007;bool v[N];int s[11][N/2];int main(){ int t,i,j; __int64 sum,n,m,ans; scanf("%d",&t); while(t--) { memset(v,true,sizeof(v)); memset(s,0,sizeof(s)); scanf("%I64d%I64d",&n,&m); sum=(1+n)*n/2; if(sum%m||n*m>sum) { puts("NO"); continue; } puts("YES"); sum=sum/m; for(i=1;i<=m;i++) { ans=0; for(j=n;j>=1;j--) { if(v[j]&&ans+j<=sum) { ans+=j; s[i][++s[i][0]]=j; v[j]=false; } if(ans==sum) break; } } for(i=1;i<=m;i++) { for(j=0;j<s[i][0];j++) printf("%d ",s[i][j]); printf("%d\n",s[i][j]); } } return 0;}
歡迎大家給我提意見
菜鳥成長記
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。