A.Math Problem(CF 1262 A)

题目大意:给定n条线段,求一条线段,使得这个线段能够跟所有给定的线段都相交(端点值一样也算相交),最小化它的长度,可以是0.

很显然找出这n条线段的左端点最大值和右端点的最小值,它们的差和0的最大值即为答案。

 

 1 #include <bits/stdc++.h>
 2 #define MIN(a,b) (((a)<(b)?(a):(b)))
 3 #define MAX(a,b) (((a)>(b)?(a):(b)))
 4 using namespace std;
 5 
 6 template <typename T>
 7 void read(T &x) {
 8     int s = 0, c = getchar();
 9     x = 0;
10     while (isspace(c)) c = getchar();
11     if (c == 45) s = 1, c = getchar();
12     while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
13     if (s) x = -x;
14 }
15 
16 template <typename T>
17 void write(T x, char c = ' ') {
18     int b[40], l = 0;
19     if (x < 0) putchar(45), x = -x;
20     while (x > 0) b[l++] = x % 10, x /= 10;
21     if (!l) putchar(48);
22     while (l) putchar(b[--l] | 48);
23     putchar(c);
24 }
25 
26 void Input(void) {
27     int n,l,r;
28     l=0;
29     r=1e9+7;
30     read(n);
31     for(int u,v,i=1;i<=n;++i){
32         read(u);
33         read(v);
34         l=MAX(l,u);
35         r=MIN(v,r);
36     }
37     printf("%d\n",MAX(l-r,0));
38 }
39 
40 void Solve(void) {}
41 
42 void Output(void) {}
43 
44 main(void) {
45     int kase;
46     freopen("input.txt", "r", stdin);
47     freopen("output.txt", "w", stdout);
48     read(kase);
49     for (int i = 1; i <= kase; i++) {
50         //printf("Case #%d: ", i);
51         Input();
52         Solve();
53         Output();
54     }
55 }
神奇的代码

相关文章: