HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)

Cake

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

Total Submission(s): 965    Accepted Submission(s): 119

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, \dots, 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 (1
\le n \le 10^5, 2 \le m \le 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.

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 s_i of i-th
line is the number of cakes in i-th
part. Then s_inumbers
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

2015 Multi-University Training Contest 6

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#define LL long long
using namespace std;
const LL MAXN = 500000 + 10;
LL n, m;
LL ans[20][MAXN];
LL A[MAXN];
LL pos[MAXN];
LL tot, tar;
bool dfs(LL dep, LL now, LL u, LL c)
{
    if(now == 0)
    {
        LL k = 0;
        while(pos[k] != -1) k++;
        pos[k] = c;
        if(dfs(dep + 1, A[k], k + 1, c)) return true;
        pos[k] = -1;
        return false;
    }
    if(now == tar)
    {
        if(dep == tot) return true;
        if(dfs(dep, 0, 0, c + 1)) return true;
    }
    for(LL i=u;i<tot;i++)
    {
        if(pos[i] == -1 && now + A[i] <= tar)
        {
            pos[i] = c;
            if(dfs(dep + 1, now + A[i], i + 1, c)) return true;
            pos[i] = -1;
        }
    }
    return  false;
}
int main()
{
    LL T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        for(LL i=0;i<m;i++) for(LL j=0;j<n;j++) ans[i][j] = 0;
        if((n * (n + 1) / 2) % m != 0 || (2 * m - 1) > n)
        {
            printf("NO\n");
            continue;
        }
        while(n >= 40)
        {
            for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - i;
            for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - 2 * m + 1 + i;
            n -= 2 * m;
        }
        tot = n;
        tar = n * (n + 1) / (2 * m);
        for(LL i=0;i<tot;i++) A[i] = tot - i;
        for(LL i=0;i<tot;i++) pos[i] = -1;
        dfs(0, 0, 0, 0);
        for(LL i=0;i<tot;i++) ans[pos[i]][++ans[pos[i]][0]] = A[i];
        printf("YES\n");
        for(LL i=0;i<m;i++)
        {
            printf("%d", ans[i][0]);
            for(LL j=1;j<=ans[i][0];j++) printf(" %d", ans[i][j]);
            printf("\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-28 18:31:18

HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)的相关文章

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以

多校第六场 HDU 4927 JAVA大数类

题目大意:给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1?ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊,现在对组合算比较什么了-- import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Sca

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

HDU 4923 Room and Moor (多校第六场C题) 单调栈

Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that: Input The inp

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

2014多校第六场 1005 || HDU 4925 Apple Tree

题目链接 题意 : 给你一块n×m的矩阵,每一个格子可以施肥或者是种苹果,种一颗苹果可以得到一个苹果,但是如果你在一个格子上施了肥,那么所有与该格子相邻(指上下左右)的有苹果树的地方最后得到的苹果是两倍,如果(i,j)有一颗苹果树,(i-1,j)与(i,j+1)施了肥,那么苹果应该是1的两倍2,2的两倍4,最后是4个苹果,问你怎么安排苹果和施肥的格子使最后得到的苹果最多. 思路 : 画了图就可以看出来,苹果和苹果,肥与肥之间不要相邻就好了,所有的苹果之间都有施肥,所有施肥的格子都被苹果隔开了才能

【HDU】4923 Room and Moor(2014多校第六场1003)

Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 263    Accepted Submission(s): 73 Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0

2014多校第六场 || HDU 4927 Series 1(杨辉三角组合数)

题目链接 题意 : n个数,每操作一次就变成n-1个数,最后变成一个数,输出这个数,操作是指后一个数减前一个数得到的数写下来. 思路 : 找出几个数,算得时候先不要算出来,用式子代替,例如: 1 2 3 4 5 6 (2-1) (3-2) (4-3) (5-4)(6-5) (3-2-2+1)(4-3-3+2)(5-4-4+3)(6-5-5+4) (4-3-3+2-3+2+2-1)(5-4-4+3-4+3+3-2)(6-5-5+4-5+4+4-3) (5-4-4+3-4+3+3-2-4+3+3-2

2014 HDU多校弟六场J题 【模拟斗地主】

这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <climits> #include <cstring> #include <cmath> #inclu