http://acm.zzuli.edu.cn/problem.php?id=1812
1812: sort
Description
想必大家对排序已经很熟悉了,但是spy好像对排序不太熟悉,有一天,他看到这样一个关于排序的题目:
对于 k 个用空格分隔开的整数,依次为 n1, n2 … nk。请将所有下标不能被 3 但可以被 2 整除的数在这些数字原有的位置上进行升序排列,此外,将余下下标能被 3 整除的数在这些数字原有的位置上进行降序排列。
spy想了半天不知道怎么排序,你可以帮助他么?
Input
多组数据,每组数据一行,为k个小于1000的正整数,依次为 n1, n2 … nk。(1 <= k <= 100000)
Output
对于每组数据,输出排序后的结果。
Sample Input
1 3 4 2 10 6 8
Sample Output
1 2 6 3 10 4 8
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define N 110000
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-5
#define met(a, b) memset (a, b, sizeof (a))
int main ()
{
int b1[N], b2[N], a[N], x;
while (scanf ("%d", &x) != EOF)
{
met (b1, 0);
met (b2, 0);
met (a, 0);
a[1] = x;
int k = 1;
char ch;
while (scanf ("%c", &ch), ch!='\n')
{
if (ch == ' ') k++;
if (ch >= '0' && ch <= '9')
a[k] = a[k]*10 + ch-'0';
}
int j1 = 0, j2 = 0;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
b1[j1++] = a[i];
else if (i%3 == 0)
b2[j2++] = a[i];
}
sort (b1, b1+j1);
sort (b2, b2+j2);
j1 = 0, j2--;
for (int i=1; i<=k; i++)
{
if (i%2==0 && i%3)
a[i] = b1[j1++];
else if (i%3==0)
a[i] = b2[j2--];
}
for (int i=1; i<k; i++)
printf ("%d ", a[i]);
printf ("%d\n", a[k]);
}
return 0;
}