PAT 1056 Mice and Rice

Source: Internet
Author: User
# Include <cstdio> # include <climits> # include <cstdlib> # include <vector> # include <list> using namespace std; list <int> :: iterator group_pick (list <int> & player, list <int >:: iterator & cur, int group_size, vector <int> & W) {int wmax = INT_MIN; list <int>: iterator ret = player. end (); int cnt = group_size; // printf ("check group: \ n \ t"); while (cur! = Player. end () & cnt> 0) {-- cnt; // printf ("% d (% d)", * cur, W [* cur]); if (W [* cur]> = wmax) {wmax = W [* cur]; ret = cur;} cur ++;} // printf ("\ n "); return ret;} int main () {int N = 0, G = 0; scanf ("% d", & N, & G); if (N <1) return 0; vector <int> W (N, 0); vector <int> R (N, 0); vector <int> L; list <int> P; for (int I = 0; I <N; I ++) {scanf ("% d", & W [I]) ;}for (int I = 0; I <N; I ++) {int t = 0; scanf ("% d" , & T); P. push_back (t);} int level = 0; int level_cnt = 0; list <int> tmp; auto cur = P. begin (); // number of elements in P shoshould be larger than 1 to perform reduce processing while (G> 1 & ++ (cur = P. begin ())! = P. end () {tmp. clear (); auto cur = P. begin (); while (cur! = P. end () {list <int >:: iterator fat = group_pick (P, cur, G, W); // printf ("pick % d \ n ", * fat); tmp. splice (tmp. end (), tmp, fat);} swap (tmp, P); auto iter = tmp. begin (); while (iter! = Tmp. end () {R [* (iter ++)] = level; level_cnt ++;} L. push_back (level_cnt); level_cnt = 0; level ++;} // now there must be only one element in P, the final winner L. push_back (1); R [P. front ()] = level; int sum = 0; for (int I = L. size ()-1; I> = 0; I --) {// printf ("level cnt: % d \ n", L [I]); int next_sum = sum + L [I]; L [I] = sum + 1; sum = next_sum;} int len = R. size (); printf ("% d", L [R [0]); for (int I = 1; I <len; I ++) {printf ("% d", L [R [I]);} return 0 ;}

A little annoying

PAT 1056 Mice and Rice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.