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