您的位置:首页 > 其它

Codeforces 673B Problems for Round【思维】

2017-02-12 17:11 316 查看
B. Problems for Round

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

There are n problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are
m pairs of similar problems. Authors want to split problems between two division according to the following rules:

Problemset of each division should be non-empty.
Each problem should be used in exactly one division (yes, it is unusual requirement).

Each problem used in division 1 should be harder than any problem used in division 2.

If two problems are similar, they should be used in different divisions.
Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2in the other.

Note, that the relation of similarity is not transitive. That is, if problem
i is similar to problem
j and problem j is similar to problem
k, it doesn't follow that
i is similar to k.

Input
The first line of the input contains two integers n and
m (2 ≤ n ≤ 100 000,
0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively.

Each of the following m lines contains a pair of similar problems
ui and
vi (1 ≤ ui, vi ≤ n, ui ≠ vi).
It's guaranteed, that no pair of problems meets twice in the input.

Output
Print one integer — the number of ways to split problems in two divisions.

Examples

Input
5 2
1 4
5 2


Output
2


Input
3 3
1 22 3
1 3


Output
0


Input
3 23 13 2


Output
1


Note
In the first sample, problems 1 and
2 should be used in division 2, while problems 4 and
5 in division 1. Problem 3 may be used either in division 1 or in division 2.

In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules.

Third sample reminds you that the similarity relation is not transitive. Problem
3 is similar to both 1 and
2, but 1 is not similar to
2, so they may be used together.

题目大意:

现在一共有N个题,题目编号就是题目的难度。

此时给你M个相似的关系,表示题目编号为u的和题目编号为v的题目是相似的。

我们要求相似的题目不能分在同一组别中,现在有两个组别,一个是DIV1,另一个是DIV2.

同时要求:

两个组别不能为空。

DIV1的任意一道题都比DIV2的任意一道题难度高。

相似的题目不能在同一组别中。

思路:

这类题我们考虑极限即可:

①对于相似的两道题来讲,肯定是编号小的分到DIV2中,编号大的分到DIV1中。

②那么对于m个关系来讲,我们就能够确定下来肯定要分到DIV2中的题目数,以及肯定要分到DIV1中的题目数。

③那么我们就维护DIV2中最难的题目编号L,以及DIV中最简单的题目编号,R。

④那么肯定小于L编号的都要分到组别2中,大于R编号的都要分到组别1中。

⑤那么处于(L,R)之间的就是可以随意分配的部分。最终答案:ans=R-L.

Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int l,r;
l=1,r=n;
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
if(u>v)swap(u,v);
if(l==0&&r==0)l=u,r=v;
else
{
l=max(l,u);
r=min(r,v);
}
}
if(r-l>=0)printf("%d\n",r-l);
else printf("0\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 673B