CodeForces 558A Lala Land and Apple Trees(模拟)

题意:小女孩摘苹果,一开始小女孩在原点0,左右两边正负X轴的整数位置有若干苹果树,给出苹果树的位置和苹果树上苹果的数量。小女孩的移动方式是,开始可以向左或者向右,但摘完一颗苹果树后,必须反向行走(ex:开始向右x正轴行走, 摘完一颗苹果树,就在向左x负轴行走),若干次后,如果她所行走的哪个方向没有苹果树了(都摘过了),就结束。最多摘多少个苹果。

解题思路:使用两个结构数组分别储存正轴和负轴的苹果树的位置和苹果数量,排序,在比较两边哪边的数量多,数量少的部分会被全部摘光,数量多的部分会摘掉是数量少的数量数 + 1。

Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.

Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.

What is the maximum number of apples he can collect?

Input

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land.

The following n lines contains two integers each xiai ( - 105 ≤ xi ≤ 105xi ≠ 01 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it.

It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

Output

Output the maximum number of apples Amr can collect.

Sample test(s)
input
2
-1 5
1 5
output
10
input
3
-2 2
1 4
-1 3
output
9
input
3
1 9
3 5
7 10
output
9
Note

In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.

In the second sample test the optimal solution is to go left to x =  - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x =  - 2.

In the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.



#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

int buf[10];
//整型变量快速输入输出函数
inline int readint()
{
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c))
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

inline void writeint(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
//
#define MAX_N 105
const int INF = 0x3f3f3f3f;
struct node
{
    int x, s;
};
node a[MAX_N];//++
node b[MAX_N];//--
bool cmp1(node a, node b)
{
    return a.x < b.x;
}

bool cmp2(node a, node b)
{
    return a.x > b.x;
}

int main()
{
    int n;
    n = readint();
    int cnt1 = 0;
    int cnt2 = 0;
    for(int i = 0 ; i < n ; i++)
    {
        int num, s;

        scanf("%d%d", &num, &s);
        if(num > 0)
        {
            a[cnt1].x = num;
            a[cnt1].s = s;
            cnt1++;
        }
        else
        {
            b[cnt2].x = num;
            b[cnt2].s = s;
            cnt2++;
        }
    }
    sort(a, a + cnt1, cmp1);
    sort(b, b + cnt2, cmp2);
    int sum = 0;
    if(cnt1 == cnt2)
    {
        for(int i = 0 ; i < cnt1 ; i++)
        {
            sum += a[i].s + b[i].s;
        }
    }
    else if(cnt1 > cnt2)
    {
        for(int i = 0 ; i < cnt2 ; i++)
        {
            sum += b[i].s + a[i].s;
        }
        sum +=a[cnt2].s;
    }
    else
    {
        for(int i = 0 ; i < cnt1 ; i++)
        {
            sum += b[i].s + a[i].s;
        }
        sum += b[cnt1].s;
    }
    cout<


你可能感兴趣的:(水题)