Codeforces:Diverse Permutation(找规律)

***********************************声明*******************************

原创作品,出自 “晓风残月xj” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。

由于各种原因,可能存在诸多不足,欢迎斧正!

*******************************************************************

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Permutationp is an ordered set of integers
p1,???p2,???...,???pn, consisting ofn distinct positive integers
not larger thann. We‘ll denote as
n the length of permutation p1,???p2,???...,???pn.

Your task is to find such permutationp of length
n, that the group of numbers|p1?-?p2|,?|p2?-?p3|,?...,?|pn?-?1?-?pn|
has exactly k distinct elements.

Input

The single line of the input contains two space-separated positive integersn,
k (1?≤?k?<?n?≤?105).

Output

Print
n integers forming the permutation. If there are multiple answers, print any of them.

Sample test(s)

Input

3 2

Output

1 3 2

Input

3 1

Output

1 2 3

Input

5 2

Output

1 3 2 4 5

Note

By |x| we denote the absolute value of numberx.

题解:

本题要求给定1到n的序列,满足相邻两项之差的绝对值不相同的个数为k。由于给定的1?≤?k?<?n?≤?10范围较大,所以只能寻找时间复杂度为O(n)的算法。可以想到该序列最多有n-1个不同的相邻差(绝对值),其中一个满足条件的序列是:n,1,n-1,2,n-3,3…………。可以尝试构造满足条件的前k-1,然后后面的顺序填写。

Java源代码:

import java.util.Scanner;

public class Main {

	private int k, n;

	public Main(int tn, int tk) {
		n = tn;
		k = tk;
	}

	public void printMain() {
		boolean flag = true;
		int count = k - 1;
		int i = 1;
		while (count > 0) {
			if (count != k - 1) {
				--count;
				if (count <= 0) {
					flag = false;
					break;
				}
				System.out.print(" ");
			}

			System.out.print(i);
			System.out.print(" " + (n - i + 1));
			++i;
			--count;
		}
		int j = n - i + 1;
		if (count != k - 1 && i <= j)
			System.out.print(" ");
		if (flag) {
			while (i <= j) {
				System.out.print(j);
				--j;
				if (i <= j)
					System.out.print(" ");
			}
		} else {
			while (i <= j) {
				System.out.print(i);
				++i;
				if (i <= j)
					System.out.print(" ");
			}
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while (input.hasNext()) {
			int tn = input.nextInt();
			int tk = input.nextInt();
			Main AC = new Main(tn, tk);
			AC.printMain();
		}
		input.close();
	}
}

由于时间和水平的原因,难免有不足的地方,欢迎斧正!

时间: 2024-12-12 19:59:19

Codeforces:Diverse Permutation(找规律)的相关文章

codeforces Goodbye2018 C. New Year and the Permutation Concatenation 傻瓜解法找规律+打表

这是goodbye2018的D题,红包赛第一场的C题 是我打的第一场CF 不知道为什么每次一补到这一场我就要强调一遍这是我的第一场CF...... .... .... 真矫情 不过还挺有仪式感的,嗯嗯~ o( ̄▽ ̄)o 看题吧 这题是肯定有公式的,不然这么乱七八糟的真的很难找规律,而且题目的名字就是permutation concatenation 这个题目真的很坏,只给了n=3时ans=9:n=4时ans=56: 其实如果知道了n=5时,ans=395,问题就迎刃而解了 我的方法跟官方题解的不

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 Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis

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 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 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

Codeforces 57C Array dp暴力找规律

题目链接:点击打开链接 先是计算非递增的方案, 若非递增的方案数为x, 则非递减的方案数也是x 答案就是 2*x - n 只需求得x即可. 可以先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元即可. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #def

Codeforces 837E Vasya&#39;s Function 数论 找规律

题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b).给定 x 和 y (<=1e12)求F(x,y). 题解:a=A*GCD(a,b) b=B*GCD(a,b),那么b-GCD(a,b) = (B-1)*GCD(a,b),如果此时A和B-1依然互质,那么GCD不变下一次还是要执行b-GCD(a,b).那么GCD什么时候才会变化呢?就是说找到一个最小的S,使得(B-S)%T=0其中T是a的任意一个因子.变形得到:B%T=S于是我们知道S=min(B%T).也

构造 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 ********************