Codeforces Round #223 (Div. 2) B. Sereja and Stairs
2014-01-20 14:20
465 查看
Sereja loves integer sequences very much. He especially likes stairs.
Sequence a1, a2, ..., a|a| (|a| is
the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|),
that the following condition is met:
a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.
For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.
Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the
table?
Input
The first line contains integer m (1 ≤ m ≤ 105) —
the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000) —
the numbers on the Sereja's cards.
Output
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
输出数列为凸型
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; }
int main()
{
int a[100010],b[100010],c[100010],n,i,j,x=0;
scanf("%d",&n);
for(int k=0;k<n;++k)
scanf("%d",&a[k]);
qsort(a,n,sizeof(int),cmp);
memset(b,0,sizeof(b));
i=0;j=n-1;
x=n;
for(int k=0;k<n;)
{
if(a[k]==b[i-1]&&a[k]==b[j+1]) {++k;--x;continue;}
if(a[k]!=b[i-1])
b[i++]=a[k++];
else b[j--]=a[k++];
}
if(b[i-1]==b[j+1])
{
b[j+1]=0;
--x;
}
printf("%d\n",x);
for(int k=n-1;k>0;--k)
if(b[k])
printf("%d ",b[k]);
printf("%d\n",b[0]);
return 0;
}
Sequence a1, a2, ..., a|a| (|a| is
the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|),
that the following condition is met:
a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.
For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.
Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the
table?
Input
The first line contains integer m (1 ≤ m ≤ 105) —
the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000) —
the numbers on the Sereja's cards.
Output
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
输出数列为凸型
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; }
int main()
{
int a[100010],b[100010],c[100010],n,i,j,x=0;
scanf("%d",&n);
for(int k=0;k<n;++k)
scanf("%d",&a[k]);
qsort(a,n,sizeof(int),cmp);
memset(b,0,sizeof(b));
i=0;j=n-1;
x=n;
for(int k=0;k<n;)
{
if(a[k]==b[i-1]&&a[k]==b[j+1]) {++k;--x;continue;}
if(a[k]!=b[i-1])
b[i++]=a[k++];
else b[j--]=a[k++];
}
if(b[i-1]==b[j+1])
{
b[j+1]=0;
--x;
}
printf("%d\n",x);
for(int k=n-1;k>0;--k)
if(b[k])
printf("%d ",b[k]);
printf("%d\n",b[0]);
return 0;
}
相关文章推荐
- Codeforces Round #223 (Div. 2)——B. Sereja and Stairs
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- Codeforces Round #235 (Div. 2)B. Sereja and Contests
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
- CodeForces Round 213 Div 2 E Sereja and Brackets 线段树
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs
- Codeforces Round #243 (Div. 1)——Sereja and Squares
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
- Codeforces Round #243 (Div. 2)——Sereja and Table
- Codeforces Round #243 (Div. 1)——Sereja and Squares
- Codeforces #187 (Div. 1) B. Sereja and Periods && Hihocoder 1355 (字符串匹配倍增好题)
- codeforces #233 (Div2) A. Sereja and Dima【模拟】+B. Sereja and Stairs【暴力】
- 381-B. Sereja and Stairs
- codeforces #233 (Div2) C. Sereja and Prefixes【二分+递归】
- Codeforces Round #394 (Div. 2)Dasha and Stairs
- CodeForce H - Sereja and Stairs
- Codeforces Round #243 (Div. 2)——Sereja and Table
- Codeforces Round #243 (Div. 1)——Sereja and Two Sequences