zoukankan      html  css  js  c++  java
  • 剩下的树 THU 机试

    链接:https://www.nowcoder.com/questionTerminal/f5787c69f5cf41499ba4706bc93700a2
    来源:牛客网

    有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。     现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。     可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

    输入描述:
        两个整数L(1<=L<=10000)和M(1<=M<=100)。
        接下来有M组整数,每组有一对数字。


    输出描述:
        可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。
    示例1

    输入

    500 3
    100 200
    150 300
    470 471
    

    输出

    298
    排序就行了;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 200005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-5
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    
    inline int rd() {
    	int x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    int sqr(int x) { return x * x; }
    
    
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    int L, M;
    struct node {
    	int l, r;
    }t[maxn];
    bool cmp(node a, node b) {
    	if (a.l != b.l)return a.l < b.l;
    	return a.r < b.r;
    }
    
    int main()
    {
    	//ios::sync_with_stdio(0);
    	while (cin >> L >> M) {
    		ms(t);
    		for (int i = 1; i <= M; i++) {
    			int u, v; u = rd(); v = rd();
    			t[i].l = u; t[i].r = v;
    		}
    		sort(t + 1, t + 1 + M, cmp);
    		int ans = 0;
    		t[0].l = t[0].r = -1;
    		for (int i = 1; i <= M; i++) {
    			if (t[i].l > t[i - 1].r) {
    				ans += (t[i].r - t[i].l + 1);
    			}
    			else {
    				if (t[i].r <= t[i - 1].r) {
    					t[i].r = t[i - 1].r;
    				}
    				else {
    					ans += (t[i].r - t[i - 1].r);
    				}
    			}
    		}
    		printf("%d
    ", L + 1 - ans);
    	}
    	return 0;
    }
    
    
    
    EPFL - Fighting
  • 相关阅读:
    HDU 1010 Tempter of the Bone
    HDU 4421 Bit Magic(奇葩式解法)
    HDU 2614 Beat 深搜DFS
    HDU 1495 非常可乐 BFS 搜索
    Road to Cinema
    Sea Battle
    Interview with Oleg
    Spotlights
    Substring
    Dominating Patterns
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10347202.html
Copyright © 2011-2022 走看看