↓ Skip to main content
  1. Posts/

51nod_learn_greedy_活动安排2

·532 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室?

1第一行一个正整数n (n <= 10000)代表活动的个数。
2第二行到第(n + 1)行包含n个开始时间和结束时间。
3开始时间严格小于结束时间,并且时间都是非负整数,小于1000000000

输出

一行包含一个整数表示最少教室的个数。

输入示例

3
1 2
3 4
2 9

输出示例

2

其实就是求某个时间点的最大厚度… 一开始傻逼了… 想着什么从1开始推过去…必然tle..就没写== 然后今天再开…我只要把开始时间和结束时间放在一起排序…从小到大 然后用一个boolean做好标记就好… 有点像海洋兄的收费站的比喻?

代码实现
 1/*************************************************************************
 2> File Name: code/51nod/learn/greedy/2.cpp
 3> Author: 111qqz
 4> Email: rkz2013@126.com
 5> Created Time: 2015年10月05日 星期一 19时24分32秒
 6************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#include<cctype>
21
22#define yn hez111qqz
23#define j1 cute111qqz
24#define ms(a,x) memset(a,x,sizeof(a))
25using namespace std;
26const int dx4[4]={1,0,0,-1};
27const int dy4[4]={0,-1,1,0};
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31const int N=1E4+5;
32int n;
33struct Q
34{
35int t;
36bool sta;
37}q[2*N];
38
39bool cmp(Q a,Q b)
40{
41return a.t<b.t;
42}
43int main()
44{
45#ifndef  ONLINE_JUDGE
46freopen("in.txt","r",stdin);
47#endif
48
49scanf("%d",&n);
50for ( int i = 0 ; i < 2*n ; i++)
51{
52scanf("%d",&q[i].t);
53if (i%2==0)
54{
55q[i].sta = true;
56}
57else
58{
59q[i].sta = false;
60}
61}
62sort(q,q+2*n,cmp);
63int ans = -1;
64int cnt = 0;
65for ( int i = 0 ; i < 2*n ; i++)
66{
67if (q[i].sta)
68{
69cnt++;
70}
71else
72{
73cnt--;
74}
75ans = max(ans,cnt);
76}
77printf("%dn",ans);
78
79
80#ifndef ONLINE_JUDGE
81fclose(stdin);
82#endif
83return 0;
84}

Related

codeforces 479D. Long Jumps

·700 words·2 mins
http://codeforces.com/problemset/problem/479/D 题意是说有一把尺子,本身有一些刻度,然后需要测量x和y,问最少需要添加多少个刻度,如果需要,这些刻度分别添加在什么位置。

codeforces 525 B. Pasha and String

·423 words·1 min
http://codeforces.com/problemset/problem/525/B 1题意是说一个字符串,进行m次颠倒变换(从a[i]位置到a[l-i+1]位置),问得到的字符串。容易发现,对于越在里边(对称,也就是越靠近中间位置)的字符,调换的次数越多。我们可以把a[i]从小到大排序。然后经过分析发现,把两个相邻的a[i]分为一组,做处理,如果m为奇数,最后还剩下a[m]没有被分组,要单独处理a[m]细节上要注意st数组是从st[0]开始的...好吧的确不方便,适牛也说我了。。数组下标以后还是从0开始吧。。。主要是受高中OI用的pascal的影响。。。那个数组下标随便啊。代码: 2 3 4 5 6 /* *********************************************** 7 Author :111qqz 8 Created Time :2016年02月22日 星期一 23时39分51秒 9 File Name :code/cf/problem/525B.cpp 10 ************************************************ */ 11 12 #include <iostream> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <cstdio> 17 18 using namespace std; 19 20 int m,k,len; 21 const int N=2E5+7; 22 int a[N]; 23 char st[N]; 24 25 int main() 26 { 27 cin>>st; 28 scanf("%d",&m); 29 for ( int i = 1 ; i <= m ; i++ ) 30 scanf("%d",&a[i]); 31 sort(a+1,a+m+1); 32 k = 1; 33 len = strlen(st); 34 while (k<=m) 35 { 36 for ( int j = a[k] ; j <= a[k+1]-1 ; j++) 37 swap(st[j-1],st[len-j]); 38 k = k + 2; 39 } 40 if ( m %2==1 ) 41 for ( int i = a[m]; i <= len/2 ; i++ ) 42 swap(st[i-1],st[len-i]); 43 cout<<st<<endl; 44 return 0; 45 }

codeforces 482 A. Diverse Permutation(构造)

·385 words·1 min
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.