Codeforces 558A Lala Land and apple Trees pick apples

Source: Internet
Author: User

Test instructions: N apple trees in a row on the x-axis, each apple tree has a coordinate value (not 0) and the number of apples. Now someone chooses One direction from 0, picks up all the apples each time they meet a new apple tree, and then turn around until they meet the next apple tree that they haven't met before and then pick up all the apples, so that they can't meet the new Apple tree again. Ask how many apples can be picked off at most.





Water problem. Based on 0, the number of apple trees on the left and right sides is calculated after the coordinates are sorted. If the left side is less than the right, first pick from the left, or vice versa first from the right. Finally see how many apple trees can not pick up.







#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include < cstring> #include <string> #include <stack> #include <queue> #include <vector> #include < map>using namespace Std;const int MAX = 105;int N;int x, A, Sum;vector <pair <int, int> > L, R; Do not bother to write the structure of BOOL CMP (pair <int, int> P1, pair <int, int> p2) {return P1.first < P2.first;}    void input () {l.clear ();    R.clear ();    sum = 0;        for (int i = 0; i < n; i++) {scanf ("%d%d", &x, &a);        if (x < 0) L.push_back (Make_pair (x, a));        Else R.push_back (Make_pair (x, a));    sum + = A;    }}void solve () {int ls = l.size (), rs = R.size (); int total = ABS (LS-RS)-1;    Apple Tree Tree Sort (l.begin (), L.end (), CMP) not to be picked;    Sort (R.begin (), R.end (), CMP);    if (L.size () > R.size ()) {for (int i = 0; i < total; i++)///left-hand count sum-= L[i].second; } ELSE {for (int i = 0, i < total; i++)///Right cannot be picked up sum-= R[r.size ()-I-1].second; } printf ("%d\n", sum);}        int main () {while (scanf ("%d", &n)! = EOF) {input ();    Solve (); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 558A Lala Land and apple Trees pick apples

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.