Codeforces Round #166 (Div. 2) c. Secret

本文介绍了一种将秘密(一组单词)安全地分配给多个守护者的算法。该算法确保每个守护者获得的单词数量不少于三个,且这些单词不形成等差数列,同时所有守护者之间的单词集合互不相交,共同覆盖所有单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Secret
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presuppose that the set elements are written in the increasing order.

We'll say that the secret is safe if the following conditions are hold:

  • for any two indexes i, j (1 ≤ i < j ≤ k) the intersection of sets Ui and Uj is an empty set;
  • the union of sets U1, U2, ..., Uk is set (1, 2, ..., n);
  • in each set Ui, its elements ui, 1, ui, 2, ..., ui, |Ui| do not form an arithmetic progression (in particular, |Ui| ≥ 3 should hold).

Let us remind you that the elements of set (u1, u2, ..., us) form an arithmetic progression if there is such number d, that for all i (1 ≤ i < s) fulfills ui + d = ui + 1. For example, the elements of sets (5)(1, 10) and (1, 5, 9) form arithmetic progressions and the elements of sets (1, 2, 4) and (3, 6, 8) don't.

Your task is to find any partition of the set of words into subsets U1, U2, ..., Uk so that the secret is safe. Otherwise indicate that there's no such partition.

Input

The input consists of a single line which contains two integers n and k (2 ≤ k ≤ n ≤ 106) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.

Output

If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret.

If there are multiple solutions, print any of them.

Sample test(s)
input
11 3
output
3 1 2 1 1 2 3 2 2 3 1
input
5 2
output
-1

题意 注意原文中标红线的话就好了 

给你 n是1-n个数 k是集合的个数

是每个集合中的个数必须大于等于3,并且集合中的数从大到小排列后不能构成等差数列,每个集合不能有交集看,全部的并集为这个集合

下面就枚举每个集合中应包含那个数 输出一个结果就好了

本想这样枚举的

例子 n=11 k=3

枚举

1 2 3 3 2 1 1 2 3 3    2  

1 2 3 4 5 6 7 8 9 10 11 

但发现对于2这个集合为等差数列

说明这个例子中我这样的枚举适合偶数个集合 不适合基数的集合

那么 我就调换一下这个位置好了

1 2 3 {2 3} 1 1 2 3 3  2   

1 2 3  4 5  6 7 8 9 10 11 

post code:

#include<stdio.h>
int n,k;
void even()
{
   int i;
   while(1){   
   for(i=1;i<=k;i++){
   printf("%d ",i);
   n--;
   if(n==0)goto end;       
   }
   int add=1;
   for(;i<=2*k;i++){
       printf("%d ",i-add);
       add+=2;
       n--;
       if(n==0)goto end;                 
       }
   }     
   end:  printf("\n");     
}
void odd()
{
   int i,mid=k/2+k+1,beg=1;
   while(1){   
   for(i=1;i<=k;i++){
   printf("%d ",i);
   n--;
   if(n==0)goto end;       
   }
   int add=1,temp;
   for(;i<=2*k;i++){
       if(i==mid  &&beg==1){temp=i-add;goto point;}  //这里进行位置的调换
       if(i==mid+1&&beg==1){printf("%d %d ",i-add,temp);goto point;}
       printf("%d ",i-add);
 point:add+=2;
       n--;
       if(n==0)goto end;                 
       }
   beg++;
   }          
   end:  printf("\n");   
}

int main()
{
   while(scanf("%d %d",&n,&k)!=EOF)
   {
        if(k*3>n)printf("-1\n");
        else {
              if(k%2==0)even();
              else odd();
            } 
            
                   
   }    
    
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值