题面:
在一场比赛中,双方的比分是不断变化的,而这期间总有一些时刻是持平。输入一个数n,然后接下来n行每行给出两个数a,b,代表此时比分为a:b。给出的比分是按照时间顺序的。求:直到最后一组比分为止,最多可能有多少次比分相同?
样例1:
输入:
3
2 0
3 1
3 4
输出:
2
样例解释:一种可能的比赛过程为:
0:0 1:0 2:0(input) 3:0 3:1(input) 3:2 3:3 3:4(input)
整场比赛过程中平局有2次。
样例2:
输入:
3
0 0
0 0
0 0
输出:
1
样例解释:0:0一次
样例3:
输入:
1
5 4
输出:
4
解题思路:
这哪有什么固定思路,随便怎么模拟都能过。
但是看到大佬代码之后还是会觉得自己菜得真实,巧妙地解决掉了分类讨论的部分吧:
#include <cstdio>
#include <cctype>
#include <iostream>
#include <algorithm>
#define ri readint()
#define gc getchar()
using namespace std;
int readint() {
int x = 0, s = 1, c = gc;
while (c <= 32) c = gc;
if (c == '-') s = -1, c = gc;
for (; isdigit(c); c = gc)
x = x * 10 + c - 48;
return x * s;
}
int n, a, b, lasta, lastb;
long long ans;
int main() {
for (n = ri; n; n--) {
a = ri, b = ri;
ans += max(0, min(a, b) - max(lasta, lastb) + 1);
lasta = a, lastb = b;
if (lasta == lastb) {
lasta++;
lastb++;
}
}
cout << ans << endl;
return 0;
}