bzoj1136: [POI2009]Arc

Description

给定一个序列{ai | 1 <= ai <= 1000000000 且 1 <= i <= n 且 n <= 15000000}和一个整数 k (k <= n 且 k <= 1000000),求出a的一个长度为k的子序列{a[bi]}满足: (1) 1 <= b1 <= b2 <= ... <= bk (2) 在满足(1)的情况下 {a[b1], a[b2], ... , a[bk]} 字典序最大。

Input

第一行一个数k,以下一行,为序列ai。以一个单独的0结束

Output

k行,每行一个数,其中第i行为a[bi]。

Sample Input

12
5 8 3 15 8 0

Sample Output

12
15
8

 

题解:

http://blog.csdn.net/popoqqq/article/details/44812723

code:

  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 #include <time.h>
  4  
  5 #define MAGIC_BEGIN -435634223
  6 #define MAGIC_END -324556462
  7  
  8 #define MIN_K 1
  9 #define MAX_K 1000000
 10 #define MAX_N 15000000
 11 #define MIN_A 1
 12 #define MAX_A 1000000000
 13 #define MIN_TYP 1
 14 #define MAX_TYP 3
 15 #define MIN_PAR 0
 16 #define MAX_PAR 1000000000
 17  
 18 #define ERROR 0
 19 #define CORRECT 1
 20  
 21 #define unlikely(x) __builtin_expect(!!(x), 0)
 22  
 23 static int init = 0; // czy zostala juz wywolana funkcja inicjuj()
 24 static int lib_n; // ile biblioteka podala juz liczb
 25 static int con_k; // ile zawodnik podal liczb
 26  
 27 static int N, K, A, TYP, PAR; // parametry testu wczytywane z pliku
 28 static int bre, len_sub, bou, is_end; // zmienne pomocnicze
 29  
 30 static int rand2_status = 198402041;
 31  
 32 static inline int rand2(int a, int b){
 33   rand2_status = rand2_status * 1103515245 + 12345;
 34   int x = rand2_status;
 35   if (x < 0) x = -x; // -2^31 sie nie zdarza :D
 36   x >>= 1;
 37   x = a + x % (b - a + 1);
 38   return x;
 39 }
 40  
 41 /* test losowy */
 42 static inline int random_test()
 43 {
 44     return rand2(1, A);
 45 }
 46  
 47 /* test z dlugim podciagiem nierosnacym */
 48 static inline int decreasing_test()
 49 {
 50     int tmp;
 51     if(bre == 0) {
 52         bre = rand2(0, (N - lib_n + 1 - len_sub));
 53         tmp = A;
 54         A -= rand2(0, (A - 1) / len_sub);
 55         len_sub--;
 56     }
 57     else {
 58         bre--;
 59         tmp = rand2(1, A);
 60     }
 61     return tmp;
 62 }
 63  
 64 /* test z dlugim podciagiem niemalejacym */
 65 static inline int increasing_test()
 66 {
 67     return bou - decreasing_test();
 68 }
 69  
 70 static void finish(int res, char *com)
 71 {
 72     if(res == ERROR)
 73         printf("%s\n", com);
 74     exit(0);
 75 }
 76  
 77 /* Inicjuje dane wejsciowe i zwraca liczbe projektow */
 78 int inicjuj()
 79 {
 80     if(init == 1)
 81         finish(ERROR, "Program zawodnika moze wywolac funkcje inicjuj tylko raz!!!");
 82     init = 1;
 83     scanf("%d", &K);
 84     if (K > 0){
 85       TYP = 0;
 86       N = MAX_N + 2;
 87       return K;
 88     }
 89     int magic_begin, magic_end;
 90     scanf("%d%d", &magic_begin, &TYP);
 91     if(magic_begin != MAGIC_BEGIN || TYP < MIN_TYP || TYP > MAX_TYP)
 92         finish(ERROR, "Program zawodnika nie moze korzystac z stdin!!!");
 93     scanf("%d%d%d%d", &N, &K, &A, &PAR);
 94     if(N < 1 || N > MAX_N || N < K || K > MAX_K || A < MIN_A || A > MAX_A 
 95         || PAR < MIN_PAR || PAR > MAX_PAR)
 96         finish(ERROR, "Program zawodnika nie moze korzystac z stdin!!!");
 97     scanf("%d", &magic_end);
 98     if(magic_end != MAGIC_END)
 99         finish(ERROR, "Program zawodnika nie moze korzystac z stdin!!!");
100     con_k = 0;
101     lib_n = 0;
102     is_end = 0;
103     if(TYP == 2 || TYP == 3) {
104         len_sub = PAR;
105         bre = 0;
106     }
107     if(TYP == 2)
108         bou = A--;
109     return K;
110 }
111  
112 /* Sluzy do wczytania ciagu reprezentujacego jakosci projektow */
113 int wczytaj()
114 {
115     if(unlikely(init == 0))
116         finish(ERROR, "Program zawodnika nie wywolal funkcji inicjuj!!!");
117     if(unlikely(lib_n > N || is_end == 1))
118         finish(ERROR, "Program zawodnika wywolal funkcje wczytaj po otrzymaniu informacji o koncu ciagu!!!");
119     if(unlikely(lib_n == N))
120         return 0;
121     lib_n++;
122     switch (TYP) {
123       case 0:
124         scanf("%d", &A);
125         if(A == 0)
126           is_end = 1;
127         return A;
128         break;
129       case 1: return random_test(); break;
130       case 2: return increasing_test(); break;
131       case 3: return decreasing_test(); break;
132       default:
133               finish(ERROR, "Nieznany typ testu");
134     }
135     return -1;
136 }
137  
138 /* Sluzy do wypisania wyznaczonego podciagu */
139 void wypisz(int jakoscProjektu)
140 {
141     if(init == 0)
142         finish(ERROR, "Program zawodnika nie wywolal funkcji inicjuj!!!");
143     printf("%d\n", jakoscProjektu);
144     if(++con_k == K)
145         finish(CORRECT, "");
146 }
147 #include<cstdio>
148 #include<iostream>
149 #include<cmath>
150 #include<cstring>
151 #include<algorithm>
152 using namespace std;
153 char ch;
154 bool ok;
155 void read(int &x){
156     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
157     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
158     if (ok) x=-x;
159 }
160 const int maxk=1000010;
161 int k,n,x,a[maxk];
162 struct Data{
163     int head,tail,val[maxk];
164     void init(){head=1,tail=0;}
165     void push(int v){
166         while (head<=tail&&val[tail%maxk]<v) tail--;
167         if (tail-head+1<k) val[(++tail)%maxk]=v;
168     }
169     void pop(){head++;}
170     int front(){return val[head%maxk];}
171 }que;
172 int main(){
173     que.init(),k=inicjuj();
174     for (n=0;n<k;n++) a[n]=wczytaj(); n--;
175     while (x=wczytaj(),x){
176         a[(++n)%maxk]=x;
177         que.push(a[(n-k)%maxk]);
178     }
179     for (int i=k-1;i>=0;i--) que.push(a[(n-i)%maxk]),wypisz(que.front()),que.pop();
180     return 0;
181 }

 

posted @ 2016-03-09 11:03  chenyushuo  阅读(389)  评论(0编辑  收藏  举报