剩下的樹 THU 機試
阿新 • • 發佈:2019-02-02
個數 stack 現在 blank str syn ctime -h tchar
鏈接: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\n", L + 1 - ans); } return 0; }
剩下的樹 THU 機試