这个,用set:
/* ID: qq104801 LANG: C++ TASK: humble */ #include <iostream> #include <fstream> #include <cstring> #include <vector> #include <list> #include <set> #include <queue> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define NMAX 11111 int k,n; int h[101]; set<long int> ss; void test() { freopen("humble.in","r",stdin); freopen("humble.out","w",stdout); cin>>k>>n; for(int i=1;i<=k;i++) { cin>>h[i]; ss.insert(h[i]); } for(int i=1;i<=k;i++) { set<long int>::iterator it=ss.begin(); while(true) { long int temp=(*it)*h[i]; if(temp<0)break; if(ss.size()>n) { ss.erase(--ss.end()); if(temp>(*(--ss.end())))break; } ss.insert(temp); it++; } } cout<<*(--ss.end())<<endl; } int main () { test(); return 0; }
test data:
USACO Training Grader Results 11 users online CHN/5 IND/1 IRN/1 SVK/1 USA/2 YUG/1 USER: cn tom [qq104801] TASK: humble LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 3508 KB] Test 2: TEST OK [0.003 secs, 3508 KB] Test 3: TEST OK [0.003 secs, 3508 KB] Test 4: TEST OK [0.019 secs, 3640 KB] Test 5: TEST OK [0.027 secs, 4036 KB] Test 6: TEST OK [0.103 secs, 5752 KB] Test 7: TEST OK [0.041 secs, 4168 KB] Test 8: TEST OK [0.035 secs, 4168 KB] Test 9: TEST OK [0.005 secs, 3508 KB] Test 10: TEST OK [0.005 secs, 3508 KB] Test 11: TEST OK [0.005 secs, 3508 KB] Test 12: TEST OK [0.240 secs, 5752 KB] All tests OK. YOUR PROGRAM ('humble') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations. Here are the test data inputs: ------- test 1 ---- 2 7 3 5 ------- test 2 ---- 4 19 2 3 5 7 ------- test 3 ---- 1 20 2 ------- test 4 ---- 6 10000 2 3 5 7 11 13 ------- test 5 ---- 6 25000 2 3 5 7 11 13 ------- test 6 ---- 8 100000 2 3 5 7 11 13 17 19 ------- test 7 ---- 7 30000 2 3 5 7 11 17 23 ------- test 8 ---- 7 28888 2 3 5 11 17 23 31 ------- test 9 ---- 1 30 2 ------- test 10 ---- 4 300 11 17 23 31 ------- test 11 ---- 5 300 11 17 19 23 31 ------- test 12 ---- 100 100000 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 Keep up the good work! Thanks for your submission!