Pashmak and Buses(构造)

题目链接:http://codeforces.com/problemset/problem/459/C

题意:n个人, k辆车, d天,每天将所有 任意人安排到k辆车, 问怎样安排, 可时不存在 2人或2人以上 d天都在一起。

题解:可以构造一个数组A, n行d列,0<a[i][j] <= k. 不存在两行完全相等,由此可得 k^d < n 可以构造以个k进制数, 从0 加到n   

          不得不说智商是硬伤T T,看了赛后题解才恍然大悟。

 1 /***Good Luck***/
 2 #define _CRT_SECURE_NO_WARNINGS
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <cstring>
 7 #include <string>
 8 #include <algorithm>
 9 #include <stack>
10 #include <map>
11 #include <queue>
12 #include <vector>
13 #include <set>
14 #include <functional>
15 #include <cmath>
16 
17 #define Zero(a) memset(a, 0, sizeof(a))
18 #define Neg(a)  memset(a, -1, sizeof(a))
19 #define All(a) a.begin(), a.end()
20 #define PB push_back
21 #define inf 0x3f3f3f3f
22 #define inf2 0x7fffffffffffffff
23 #define ll long long
24 using namespace std;
25 //#pragma comment(linker, "/STACK:102400000,102400000")
26 void get_val(int &a) {
27     int value = 0, s = 1;
28     char c;
29     while ((c = getchar()) == ' ' || c == '\n');
30     if (c == '-') s = -s; else value = c - 48;
31     while ((c = getchar()) >= '0' && c <= '9')
32         value = value * 10 + c - 48;
33     a = s * value;
34 }
35 const int maxn = 1005;
36 int ret[maxn][maxn];
37 int n, k, d;
38 
39 
40 
41 int main() {
42     //freopen("data.out", "w", stdout);
43     //freopen("data.in", "r", stdin);
44     //cin.sync_with_stdio(false);
45     cin >> n >> k >> d;
46     bool flag = false;
47     int tmp = 1;
48     for (int i = 0; i <= d; ++i) 
49     if (tmp < n) tmp *= k;
50     else {
51         flag = true;
52         break;
53     }
54     if (!flag) {
55         cout << -1 << endl;
56         return 0;
57     }
58     int jz = 0;
59     for (int i = 2; i <= n; ++i) {
60         jz = 1;
61         for (int j = 1; j <= d; ++j) {
62             ret[i][j] = (ret[i - 1][j] + jz) % k;
63             if (ret[i][j] < ret[i - 1][j]) jz = 1;
64             else jz = 0;
65         }
66     }
67     for (int i = 1; i <= d; ++i) {
68         for (int j = 1; j < n; ++j) {
69             printf("%d ", ret[j][i] + 1);
70         }
71         printf("%d\n", ret[n][i] + 1);
72     }
73     return 0;
74 }

 

posted @ 2014-08-21 20:17  yeahpeng  阅读(256)  评论(0编辑  收藏  举报