列表

详情


NC222153. IsTheOrderARabbit??

描述

In a remote place, there is a rabbits market where rabbits can be bought and sold in every day. The trading rules in this market are as follows:

1. The market is available only in morning and afternoon in one day. And the rabbit transaction price may be different every day even different between morning and afternoon in a day.
2. It is prohibited that purchasing more than once in a day. And only one rabbit can be purchased at a time.
3. You can only sell once in a day, but you can sell rabbits unlimited quantities (provided that you have so many rabbits)

Mr.Cocktail has traded in this market for days, suppose he has unlimited money to buy rabbits. Before the first day and after the last day, he has no rabbits. Now, in these days, how much money did he earn the most?

输入描述

The first line contains a positive integer , which means there are  days in total .

Next, there are lines, each line contains two positive integers, representing the rabbit price in the morning and afternoon of the day .

输出描述

Output an integer on a line to indicate the answer. 

示例1

输入:

3
1 6
2 3
7 1

输出:

11

说明:

In example 1, a rabbit was purchased for $1 in the morning of the first day, and a rabbit was purchased for $2 in the afternoon of the second day. On the morning of the third day, the two rabbits were sold, a total profit of $11.

示例2

输入:

2
5 4
3 2

输出:

0

说明:

The price of the rabbit in sample 2 continues to decrease, and it is impossible to make money through buying and selling, so choose not to make any buying and selling, and output the answer 0.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 81ms, 内存消耗: 1912K, 提交时间: 2021-05-22 16:00:27

#include <iostream>
using namespace std;
int main ()
{
	long long n,i,sum,max,a[100010],b[100010];
	cin>>n;
	for(i=1;i<=n;i++) cin>>a[i]>>b[i];
	max=b[n];
	for(i=n;i>0;i--)
	{
		if(b[i]>=max)
		{
			max=b[i];
			if(a[i]>=max) max=a[i];
			else sum=sum+max-a[i];
		}
		else if (a[i]>=max)
		{
			sum=sum+max-b[i];
			max=a[i];
		}
		else
		{
			if(a[i]>=b[i]) sum=sum+max-b[i];
			else sum=sum+max-a[i];
		}
	}
	cout<<sum;
	return 0;
}

上一题