NC222153. IsTheOrderARabbit??
描述
输入描述
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; }