跳过正文
  1. Posts/

codeforces 534 A. Exam

·2 分钟

A. Exam

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.

Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.

Input

A single line contains integer n (1 ≤ n ≤ 5000) – the number of students at an exam.

Output

In the first line print integer k – the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.

In the second line print k distinct integers _a_1, a_2, …, a__k (1 ≤ a__i ≤ n), where a__i is the number of the student on the i-th position. The students on adjacent positions mustn’t have adjacent numbers. Formally, the following should be true: |a__i - a__i + 1| ≠ 1 for all i from 1 to_k - 1.

If there are several possible answers, output any of them.

题意是有n个数(1..n),问构造一个最长的数列,使得相邻元素的差的绝对值不等于1.

1,2,3,4特判下。

剩下的直接先走奇数,后走偶数构造即可。

 1
 2
 3   /* ***********************************************
 4   Author :111qqz
 5   Created Time :2016年02月22日 星期一 23时46分25秒
 6   File Name :code/cf/problem/534A.cpp
 7   ************************************************ */
 8
 9   #include <iostream>
10   #include <algorithm>
11   #include <cstdio>
12   #include <cmath>
13   #include <cstring>
14   using namespace std;
15   const int N=5E3+7;
16   int a[N],n,k,tmp;
17
18   int main()
19   {
20       cin>>n;
21       memset(a,0,sizeof(a));
22       if (n<=2)
23       {
24
25           k = 1;
26           a[1] = 1;
27           cout<<k<<endl;
28           cout<<a[1];
29           return 0;
30       }
31       if (n==3)
32       {
33           k = 2;
34           a[1] = 1;
35           a[2] = 3;
36           cout<<k<<endl;
37           cout<<a[1]<<" "<<a[2];
38           return 0;
39       }
40       if (n==4)
41       {
42           k = 4;
43           a[1] = 2;
44           a[2] = 4;
45           a[3] = 1;
46           a[4] = 3;
47           cout<<k<<endl;
48           cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4];
49           return 0;
50       }
51        tmp = (n+1)/2;
52       for ( int i = 1 ; i <= n ; i++)
53       {
54
55           if (i<=tmp)
56           {
57               a[i] = 2*i-1;
58           }
59           else
60           {
61               a[i]=a[i-tmp]+1;
62           }
63       }
64       cout<<n<<endl;
65       for ( int i = 1 ; i <= n ; i++ )
66           cout<<a[i]<<" ";
67
68
69
70       return 0;
71   }

相关文章

codeforces 482 A. Diverse Permutation(构造)

·1 分钟
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.

codeforces 479D. Long Jumps

·2 分钟
http://codeforces.com/problemset/problem/479/D 题意是说有一把尺子,本身有一些刻度,然后需要测量x和y,问最少需要添加多少个刻度,如果需要,这些刻度分别添加在什么位置。

codeforces 525 B. Pasha and String

·1 分钟
http://codeforces.com/problemset/problem/525/B 1题意是说一个字符串,进行m次颠倒变换(从a[i]位置到a[l-i+1]位置),问得到的字符串。容易发现,对于越在里边(对称,也就是越靠近中间位置)的字符,调换的次数越多。我们可以把a[i]从小到大排序。然后经过分析发现,把两个相邻的a[i]分为一组,做处理,如果m为奇数,最后还剩下a[m]没有被分组,要单独处理a[m]细节上要注意st数组是从st[0]开始的...好吧的确不方便,适牛也说我了。。数组下标以后还是从0开始吧。。。主要是受高中OI用的pascal的影响。。。那个数组下标随便啊。代码: 2 3 4 5 6 /* *********************************************** 7 Author :111qqz 8 Created Time :2016年02月22日 星期一 23时39分51秒 9 File Name :code/cf/problem/525B.cpp 10 ************************************************ */ 11 12 #include <iostream> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <cstdio> 17 18 using namespace std; 19 20 int m,k,len; 21 const int N=2E5+7; 22 int a[N]; 23 char st[N]; 24 25 int main() 26 { 27 cin>>st; 28 scanf("%d",&m); 29 for ( int i = 1 ; i <= m ; i++ ) 30 scanf("%d",&a[i]); 31 sort(a+1,a+m+1); 32 k = 1; 33 len = strlen(st); 34 while (k<=m) 35 { 36 for ( int j = a[k] ; j <= a[k+1]-1 ; j++) 37 swap(st[j-1],st[len-j]); 38 k = k + 2; 39 } 40 if ( m %2==1 ) 41 for ( int i = a[m]; i <= len/2 ; i++ ) 42 swap(st[i-1],st[len-i]); 43 cout<<st<<endl; 44 return 0; 45 }

codeforces 47 C. Exams

·1 分钟
http://codeforces.com/problemset/problem/479/C 1/************************************************ 2Author :111qqz 3Created Time :2016年02月22日 星期一 23时31分10秒 4File Name :code/cf/problem/479C.cpp 5************************************************ */ 6 7#include <iostream> 8#include <algorithm> 9#include <cstring> 10#include <cstdio> 11 12#include <cmath> 13 14using namespace std; 15int n,ans; 16const int N=1E4+5; 17int a[N],b[N]; 18 19struct Q 20{int a,b; 21}q[N]; 22 23bool cmp(Q x, Q y) 24{ 25 if ( x.a<y.a) return true; 26 if ( x.a==y.a &&x.b<y.b ) return true; 27 return false; 28} 29 30int main() 31{ 32 scanf("%d",&n); 33 for ( int i = 1 ; i <= n ; i++ ) 34 scanf("%d %d",&q[i].a,&q[i].b); 35 sort(q+1,q+n+1,cmp); 36 37 ans=q[1].b; 38 for ( int i = 2 ; i <= n; i++ ) 39 { 40 if ( q[i].b>=ans ) 41 ans = q[i].b; 42 else ans = q[i].a; 43 } 44 printf("%d\n",ans); 45 return 0; 46}