HDU多校1003-Divide the Stones(构造)

Problem Description
There are n stones numbered from 1 to n.
The weight of the i-th stone is i kilograms. We divide the stones into k groups.
Each group consists of exactly stones.
We define the weight of each group is sum of the stones’ weights in the group.
Can we divide the stones so that the weights of all groups are same?
 

 

Input
The first line of input contains an integer T (1 <= T <= 100) denoting the number of test cases.
Each test case consists of one line containing two integers n (1 ≤ n ≤ 100000), k (k is divisor of n).
It is guaranteed that the sum of n overall test cases does not exceed 500000.
 

 

Output
For each test case, if you can’t divide into k groups satisfying condition, print “no”.
Else if you can divide into k groups satisfying condition, print “yes” in one line and then print k lines.
The i-th line represent the indices of stones belonging to the i-th group.
If there are multiple solutions, you can print any of them.
 

 

Sample Input
1 4 2
 

 

Sample Output
yes 1 4 2 3
 

 

Source
 

 

Recommend
chendu   |   We have carefully selected several similar problems for you:   6623  6622  6621  6620  6619 
这个题先用求和公式对(1——n)求和 然后判断是否能进行整分,不行就输出no 可以就输出yes 
对于打印的时候怎么构造是要分情况讨论的若n/k是偶数 则按列进行蛇皮填 若n/k是奇数 要单独处理前两列后面进行蛇皮排 但也是前两列是这个题的难点
 前两列 你要实现把 1----2k个数都用上 并且保证每行之和能够递增+1 这时候我们可以通过等差数列的性质 然后求出第一行的值然后赋值为 1 和 另一个可以求出来的数 然后前者+(k/2+1) -(k/2)...... 后者-(k/2)+(k/2+1).....就把这个题的构造实现了

 

代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;

typedef long long ll;
using namespace std;
vector<int>vec[maxn];
int vis[maxn];
int main()
{
    int T;
    cin>>T;
    ll n,k;
    while(T--)
    {
        scanf("%lld%lld",&n,&k);
        ll  sum=(n*(n+1)/2);
        for(int t=1; t<=k; t++)
        {
            vec[t].clear();
        }
        memset(vis,0,sizeof(vis));
        if(sum%k!=0)
        {
            puts("no");
        }
        else
        {
            if((n/k)%2==0)
            {
                puts("yes");
                int cnt=1;
                for(int j=1; j<=n/k; j++)
                {
                    if(j%2==1)
                    {
                        for(int t=1; t<=k; t++)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                    else
                    {
                        for(int t=k; t>=1; t--)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                }
                for(int t=1; t<=k; t++)
                {
                    for(int j=0; j<vec[t].size(); j++)
                    {
                        if(j!=vec[t].size()-1)
                            printf("%d ",vec[t][j]);
                        else
                        {
                            printf("%d",vec[t][j]);
                        }
                    }
                    printf("\n");
                }
            }
            else
            {
                puts("yes");
                if(n==1)
                {
                    puts("1");
                    continue;
                }
                ll kk=(3*k+3)/2;
                ll ss=k/2+1;
                
                int cnt=1;
                vec[1].push_back(cnt);
                vec[1].push_back(kk-cnt);
                int cnt1=cnt;
                int cnt2=kk-cnt;
                for(int t=2;t<=k;t++)
                {
                    if(t%2==0)
                    {
                    int temp1=cnt1+ss,temp2=cnt2-abs(k-ss);
                    vec[t].push_back(temp1);
                    vec[t].push_back(temp2);
                    cnt1=temp1;
                    cnt2=temp2;
                    }
                    else
                    {
                    int temp1=cnt1-abs(k-ss),temp2=cnt2+ss;
                    vec[t].push_back(temp1);
                    vec[t].push_back(temp2);
                    cnt1=temp1;
                    cnt2=temp2;
                    }
                    
                }
                cnt=2*k+1;
                for(int j=1; j<=n/k-2; j++)
                {
                    if(j%2==0)
                    {
                        for(int t=1; t<=k; t++)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                    else
                    {
                        for(int t=k; t>=1; t--)
                        {
                            vec[t].push_back(cnt++);
                        }
                    }
                }
                for(int t=1; t<=k; t++)
                {
                    for(int j=0; j<vec[t].size(); j++)
                    {
                        if(j!=vec[t].size()-1)
                            printf("%d ",vec[t][j]);
                        else
                        {
                            printf("%d",vec[t][j]);
                        }
                    }
                    printf("\n");
                }
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Staceyacm/p/11280622.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值