題目:
You still have partial information about the score during the historic football match. You are given a set of pairs?(ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai:?bibi". It is known that if the current score is ?xx:yy?, then after the goal it will change to "x+1x+1:yy" or "xx:y+1y+1". What is the largest number of times a draw could appear on the scoreboard?
The pairs "aiai:bibi" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.
Input
The first line contains a single integer?nn?(1≤n≤100001≤n≤10000) — the number of known moments in the match.
Each of the next?nn?lines contains integers?aiai?and?bibi?(0≤ai,bi≤1090≤ai,bi≤109), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).
All moments are given in chronological order, that is, sequences?xixi?and?yjyj?are non-decreasing. The last score denotes the final result of the match.
Output
Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.
Examples
input
Copy
3 2 0 3 1 3 4
output
Copy
2
input
Copy
3 0 0 0 0 0 0
output
Copy
1
input
Copy
1 5 4
output
Copy
5
Note
In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.
題目大意:先輸入一個整數n,再接著輸入n組數代表比賽過程中的比分,求在存在要求的比分下最多能有多少場平局,注:0:0也算一場平局。
解決方法:由第一次給出的比分可確定在此之前的平局總數最大為ans=min(x,y)+1,此后從第二次開始到結束,設當前比分為x:y,前一次比分為lx:ly,由此可判斷,若x<ly||y<ly,則證明這兩次之間無平局出現,若lx=ly,則平局數為ans+=min(x-lx,y-ly),若lx>ly,則ans+=min(x,y)-lx+1,若lx<ly,則ans+=min(x,y)-ly+1。
AC代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n;cin>>n;int ans=1;int x,y,lx,ly;cin>>x>>y;ans+=min(x,y);lx=x;ly=y;rep(i,2,n) {cin>>x>>y;if(x<ly||y<lx) {lx=x;ly=y;continue;}if(lx==ly) ans+=min(x-lx,y-ly);else if(lx>ly) {if(x>=y) ans+=y-lx+1;else ans+=x-lx+1;}else {if(y>=x) ans+=x-ly+1;else ans+=y-ly+1;}lx=x;ly=y;}cout<<ans<<endl;return 0;
}
?