BNU33650:Negative People in Da House

The following math joke is presented for your amusement:Two mathematicians sit in a car outside a house. Two people enter the house. Then, three people are observed going out of the house. One of the mathematicians exclaim:If one person is to enter now, the house will be empty!
Since you have very little sense of humor, you are to write a program that will calculate the minimum number of people there must have been there to begin with. In other words, given a sequence of groups of people leaving and entering the house, output the minimum number of people there must have been before you started stalking. After writing this program, your mathematician friend will leave you, as well as their math department, to start a company specializing in joke-telling and stalking.

Input

The fi rst line of the input consists of a single integer T, the number of test cases. Each of the following T cases has two parts: First, a line containing a single integer M. Then follows M lines with two integers P1 and P2 separated by a space, where the rst one contains the number of people entering the house,then the number of people leaving the house. Note that these are two events: First, P1 people enter the house, then P2 people leave the house.

 0 < T <= 50
 0 < M <= 100
 0 <= P1; P2 <= 1000

Output

Output the minimum number of people that would have to have been inside the house at the beginning.

Sample Input

1
3
3 5
3 4
1 0

Sample Output

3

 

房间里一开始有一定的人,每次有n个人进,m个人出,算出房间内原来至少要有几个人才能满足所有要求

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
    int t,i,a,b,n,in;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans = 0;
        in = 0;
        for(i = 0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            in+=a;//房间里加了a个人
            if(in<b)
            {
                ans+=(b-in);//出的人数大于进的人数,多余的那些必须是原先就在屋子里的
                in = 0;//后面进入房间的人数清0
            }
            else
            in-=b;//往里加的人数要大于出的人数,直接减
        }
        printf("%d\n",ans);
    }

    return 0;
}


 

 

你可能感兴趣的:(水,BNU)