Codeforces 482A diverse permutation greedy

Source: Internet
Author: User

Portal: CF 482A

A. Diverse permutation time limit per test 1 second memory limit per test megabytes input standard input output Standa RD output

Permutation P is an ordered set of integers p1, p2, ..., PN, consisting of n distinct positive integers not larger t Han N. We ' ll denote asn the length of permutation P1, p2, ..., PN.

Your task is to find such permutation p 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 the space-separated positive integers n, K (1≤k < n≤105). Output

Print n integers forming the permutation. If There is 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 number x.

A full arrangement of length n, which causes the absolute value of the difference between two adjacent numbers to appear in total K values


Greedy strategy problem, the number of the former K constantly spaced from both ends, the remaining number of n-k constitute a difference of 1 arithmetic progression can be

Python3:

s = input ()
arr = s.split ()
n = Int (arr[0])
k = int (arr[1])

print (1,end= ") for
I in range (2,k+1): 
  if I% 2 = = 0:
		print (", N-I//2 + 1,end=")
	else:
		print (", (i+1)//2,end=")

if k% 2 = = 0:
	x = n- K//2
	for I in Range (k,n):
		print (", x,end=")
		x = x-1
Else:
	x = (k + 1)//2 + 1 for
	i in RA Nge (k,n):
		print (", x,end=")
		x = x + 1
print ()


/******************************************************
 * File Name:   a.cpp
 * Author:      kojimai
 * Create time:2014 October 25 Saturday 12:44 05 seconds
******************************************************/

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
# Include<iostream>
using namespace std;
int judge (int n,int cnt) {
	if (cnt%2==0)
		return N (CNT/2) +1;
	else
		return (cnt+1)/2;
}
void Solve (int x,int n)
{
	if (x%2 = = 0)
		for (int l = N-X/2; x < n; l--, x + +)
			printf ("%d", l);
	else for
		(int l = (x+1)/2 + 1; x < n; l++,x++)
			printf ("%d", l);
}
int main ()
{
	int n,k;
	scanf ("%d%d", &n,&k);
	cout<<1;
	for (int i=2;i<=k;i++)
	{
		printf ("%d", Judge (N,i));
	}
	Solve (k,n);
	cout<<endl;
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.