codeforces 1131B Draw!

题目链接:http://codeforces.com/contest/1131/problem/B

解题思路

       由于我们要求的是平局的最大局数,我们知道n个时刻的比分,那么,两个队伍的分数变化区间[s1,score1[i] ]  [s2,score2[i] ] 的交集[ max(s1,s2), min(score1[i] , score2[i]) ]就是出现的平局分数的集合,那么区间的长度就是平局的分数的总数,不过记得要去重一下。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int Max = 3e5 + 10;
const int inf = 0x3f3f3f3f;

int score1[Max], score2[Max];

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		score1[0] = 0, score2[0] = 0;
		for(int i= 1 ; i <= n ; i++)
		{
			scanf("%d%d", score1+i, score2+i);
		}
		int s1 = 0, s2 = 0;	//记录上一时间的比分
		int last = 0;		//记录上一次平局的分数
		int sum = 1;		//(0,0)先计算了
		for(int i = 1 ;i <= n ; i++)
		{
			int a = max(s1, s2);
			int b = min(score1[i], score2[i]);//找的两个队伍比分的交集,区间的长度就是平局的次数,不过需要记得去重
			if (a > b)
			{
				//没有交集,也就是没有平局
			}
			else if(a == last)//判重,之前计算过的不可以重复计算
			{
				sum += b - a;
			}
			else
			{
				sum += b - a + 1;
			}
			last = b;
			s1 = score1[i];
			s2 = score2[i];
		}
		printf("%d\n", sum);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值