您的位置:首页 > 其它

CodeForces 534A-A - Exam-构造水题

2015-10-24 19:21 465 查看
给你一个n,表示有n个学生,编号1-n

你给她们安排座位,尽量使得编号相邻的两个人不在一起做

输出最多使多少人与前后编号不相邻,且输出构造得到的序列

思路:

发现只要特判n=1 2 3 4的情况,剩下的情况都是一定可以构造使得全部人不相邻

构造方法很多种,例如直接让 1  n/2+1  2  n/2+2  3 n/2+3 这样就可以了。。。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

int tm[5005];
int tmp[5005];
int cmp(int a,int b)
{return a>b;}
int main()
{
int n;
int i;
scanf("%d",&n);
int ok=0;
int half=n/2;
if (n%2) half++;
int flag;
for (i=1;i<=n/2;i++)
{
tm[++ok]=i;
tm[++ok]=half+i;
}

if (n%2)
tm[++ok]=n/2+1;
flag=0;

for (i=1;i<n;i++)
{
if (abs(tm[i]-tm[i+1])==1)
{
flag=i;break;
}
}
if (n==1)
{
printf("1\n1\n");
}
else
if (n==2)
{
printf("1\n2\n");

}
else
if (n==3)
{
printf("2\n1 3\n");
}
else
if (n==4)
{
printf("4\n3 1 4 2\n");
}
else
{
printf("%d\n",n);
for (i=1;i<=n;i++)
{
if (i!=1) printf(" ");
printf("%d",tm[i]);
}
printf("\n");
return 0;
}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: