uestc OJ 1849Negative People in Da House

Description

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

#include<stdio.h>
int main()
{
   int t,m,p,q,bgin,inhouse;
  scanf("%d",&t);
  while(t--){
  bgin=0;inhouse=0;
  scanf("%d",&m);
  while(m--){
   scanf("%d%d",&p,&q);
   if(q>p){
            q-=p;
          if(q>inhouse){q-=inhouse; inhouse=0; bgin+=q;}
          if(q<=inhouse) inhouse-=q;
    }
   else if(q<p)
     inhouse+=(p-q);
}
  printf("%d\n",bgin);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值