Codeforces Round #166 (Div. 2)C. Secret(构造)

本文介绍了一种将秘密(一组单词)安全地分配给多个守护者的算法。确保任意两个守护者之间没有交集,所有守护者拥有的单词集合为全部单词,且任一守护者拥有的单词不形成等差数列。
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<3*k, 无解,因为此时总有至少一个keeper至多拥有2个Words,构成等差数列,不合题意。
否则,可按以下方法分配n个word给k个keeper,a[i]=j表示第i个word分配给第j个keeper。
a[1]=a[2]=a[2k+1]=1;
a[3]=[4]=a[2k+2]=2;
...
a[2k-1]=a[2k]=a[3k]=k;
如果word有余,则令
a[3k+1]=a[3k+2]=...=1;

AC Code:
 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 int a[1000002], n, k;
 7 
 8 int main()
 9 {
10     while(scanf("%d %d", &n, &k) != EOF)
11     {
12         if(n < 3*k) puts("-1");
13         else
14         {
15             int i, j;
16             for(i = 2, j = 1; j <= k; i += 2, j++)
17                 a[i-1] = a[i] = a[2 * k + i / 2] = j;
18             for(i = 3 * k + 1; i <= n; i++)
19                 a[i] = 1;
20             printf("%d", a[1]);
21             for(int i = 2; i <= n; i++) printf(" %d", a[i]);
22             puts("");
23         }
24     }
25     return 0;
26 }

 


转载于:https://www.cnblogs.com/cszlg/archive/2013/02/18/2915518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值