构造 CodeForces 483C Diverse Permutation

题目传送门

 1 /*
 2     构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放。因为是绝对差值,从n开始一下一上,
 3         这样保证不会超出边界并且以防其余的数相邻绝对值差>k
 4 */
 5 /************************************************
 6 Author        :Running_Time
 7 Created Time  :2015-8-2 9:20:01
 8 File Name     :B.cpp
 9 *************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 typedef long long ll;
31 const int MAXN = 1e5 + 10;
32 const int INF = 0x3f3f3f3f;
33 const int MOD = 1e9 + 7;
34 bool vis[MAXN];
35 int a[MAXN];
36
37 int main(void)    {       //CodeForces 483C Diverse Permutation
38     int n, k;
39     while (scanf ("%d%d", &n, &k) == 2) {
40         if (k == 1) {
41             for (int i=1; i<=n; ++i)    {
42                 printf ("%d%c", i, (i==n) ? ‘\n‘ : ‘ ‘);
43             }
44             continue;
45         }
46         memset (vis, false, sizeof (vis));
47         int j = 1;  a[0] = n;   vis[a[0]] = true;   bool flag = false;
48         for (int i=k; i>=1; --i)    {
49             if (flag)     {
50                 a[j] = a[j-1] + i;  vis[a[j]] = true;  ++j;     flag = !flag;
51             }
52             else    {
53                 a[j] = a[j-1] - i;  vis[a[j]] = true;   j++;    flag = !flag;
54             }
55         }
56         int p = n;
57         for (int i=j; i<n; ++i) {
58             for (int k=p; k>=1; --k)    {
59                 if (!vis[k])    {
60                     a[i] = k;   vis[k] = true; p = k; break;
61                 }
62             }
63         }
64         for (int i=0; i<n; ++i)    {
65             printf ("%d%c", a[i], (i==n-1) ? ‘\n‘ : ‘ ‘);
66         }
67     }
68
69     return 0;
70 }
时间: 2024-08-17 16:43:29

构造 CodeForces 483C Diverse Permutation的相关文章

CodeForces 483C Diverse Permutation

Diverse Permutation Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 483C Description Permutationp is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive i

Codeforces 482A Diverse Permutation(构造)

题目链接:Codeforces 482A Diverse Permutation 题目大意:给定N和K,即有一个1~N的序列,现在要求找到一个排序,使得说所有的|pi?pi+1|的值有确定K种不同. 解题思路:构造,1,K+1,2,K,3,K-1,... K+2,K+3 ... N. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn

Codeforces 482 - Diverse Permutation 构造题

这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ................... \  /        \  /           \  / k          k-1          k-2 如图所示,先构造第一个数,就是1 + k, 然后接下来每个数字和上个数相差k , k -1 , k -2 这样下来,最后一个数字就是一个中间的数字,过程就

codeforces C. Diverse Permutation(构造)

题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个 |p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 思路: 保证相邻的两个数的差值的绝对值为单调递减序列..... 如果够k个了,最后将没有访问到的元素直接添加到末尾! 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 #d

Codeforces:Diverse Permutation(找规律)

***********************************声明******************************* 原创作品,出自 "晓风残月xj" 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj). 由于各种原因,可能存在诸多不足,欢迎斧正! ******************************************************************* time limit

Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/482/problem/A Description Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct posi

Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列其中相邻两项差的绝对值的个数(指绝对值不同的个数)为k个.求序列. 思路:1~k+1.构造序列前段,之后直接输出剩下的数.前面的构造可以根据,两项差的绝对值为1~k构造. AC代码: #include <stdio.h> #include <string.h> int ans[200010]; bool vis[100010]; i

CF 275(div.1) A Diverse Permutation

Diverse Permutation 题意:一个1~n的无重复整数序列,要求打印其中一种排列,使新序列相邻两项之差的绝对值去重后的个数刚好为k个     (1 <= k < n <= 10^5) 输入:n,k 输出:新序列 思路:想了一下,先考虑k最大和k最小取值时的序列排列情况 1) k最大时:k = n-1  序列:1,n,2,n-1,3,n-2.... 相邻两项绝对值差:n-1,n-2,n-3...1 2) k最小时:k = 1     序列:1,2,3...n | n,n-1,

Codeforces 1163E Magical Permutation [线性基,构造]

codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\)内所有的数,那么答案就对这个\(k\)取\(\max\).很显然这一定是上界. 考虑能不能构造出一组解.把\([1,2^k-1]\)的数拎出来插入线性基里得到一组极大线性无关组,那么显然它的\(size\)就是\(k\).由于它线性无关,所以任意选取一个子集得到的异或和都不会相同,所以考虑把\(0