码迷,mamicode.com
首页 > 其他好文 > 详细

CF932C Permutation Cycle

时间:2018-02-23 23:49:58      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:star   min   span   body   style   namespace   art   max   false   

思路:

构造。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int res[1000005];
 4 int main()
 5 {
 6     int n, a, b;
 7     while (cin >> n >> a >> b)
 8     {
 9         int maxn = max(a, b), minn = min(a, b);
10         bool flg = false;
11         int i = 0;
12         for (; i <= n; i += maxn)
13         {
14             if ((n - i) % minn == 0) { flg = true; break; }
15         }
16         if (!flg) { cout << -1 << endl; continue; }
17         int x = i / maxn, y = (n - i) / minn;
18         int cnt = 0, last = 0;
19         for (int i = 0; i < x; i++)
20         {
21             int start = cnt;
22             for (int j = 1 + i * maxn; j <= maxn + i * maxn; j++)
23             {
24                 res[cnt++] = j;
25             }
26             int tmp = res[start];
27             int j = start;
28             for (; j < cnt - 1; j++)
29             {
30                 res[j] = res[j + 1];
31             }
32             res[j] = tmp;
33         }
34         int fuck = cnt;
35         for (int i = 0; i < y; i++)
36         {
37             int start = cnt;
38             for (int j = fuck + 1 + i * minn; j <= fuck + minn + i * minn; j++)
39             {
40                 res[cnt++] = j;    
41             }
42             int tmp = res[start];
43             int j = start;
44             for (; j < cnt - 1; j++)
45             {
46                 res[j] = res[j + 1];
47             }
48             res[j] = tmp;
49         }
50         for (int i = 0; i < cnt; i++) cout << res[i] << " ";
51         cout << endl;
52     } 
53     return 0;
54 }

 

CF932C Permutation Cycle

标签:star   min   span   body   style   namespace   art   max   false   

原文地址:https://www.cnblogs.com/wangyiming/p/8463628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!