HDOJ-2037

今年暑假不AC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 44625    Accepted Submission(s): 23870

Problem Description

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

12

1 3

3 4

0 7

3 8

15 19

15 20

10 15

8 18

6 12

5 10

4 14

2 9

0

Sample Output

5

此题可以用贪心思想来解:电视节目结束的时间越小,之后可以看的电视节目越多。

即对结束时间进行排序,之后再依次判断是否符合条件。

附AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 struct Time{
 9     int s;//开始时间
10     int e;//结束时间
11 }t[100];
12
13 int main(){
14     int n;
15     while(~scanf("%d",&n)&&n!=0){
16         for(int i=0;i<n;i++){
17             scanf("%d %d",&t[i].s,&t[i].e);
18         }
19         int temp;
20         for(int i=0;i<n;i++){//冒泡排序
21             for(int j=n-1;j>=i;j--){
22                 if(t[i].e>t[j].e)
23                 {
24                     temp=t[j].e;
25                     t[j].e=t[i].e;
26                     t[i].e=temp;
27                     temp=t[j].s;
28                     t[j].s=t[i].s;
29                     t[i].s=temp;
30                 }
31             }
32         }
33         int sum=0;
34         int begin=0;//begin用来记录当前节目的结束时间
35         for(int i=0;i<n;i++){
36             if(t[i].s>=begin){//当下一个节目的开始时间大于当前节目的结束时间时,可看
37                 sum++;
38                 begin=t[i].e;
39             }
40         }
41         printf("%d\n",sum);
42     }
43     return 0;
44 } 
时间: 2024-08-09 20:12:54

HDOJ-2037的相关文章

HDOJ 2037简单的贪心算法

代码: #include<iostream> using namespace std; int main() { int n,s,t1[100],t2[100],i,t,j; while(cin>>n) { if(n==0) break; s=1; for(i=0;i<n;i++) cin>>t1[i]>>t2[i]; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(t2[i]>t2[j]) { t=

HDOJ 2037 今年暑假不AC

系统的学了一下贪心,事件的调度问题.重新写了一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2037 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int MAXN=120; 8 9 pair<int,int&g

Hdoj 1050.Moving Tables 题解

Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made

贪心 赛码 1001 Movie

题目传送门 1 /* 2 贪心:官方题解: 3 首先我们考虑如何选择最左边的一个区间 4 假设最左边的区间标号是i, 那选择的另外两个区间的左端点必定要大于Ri 5 若存在i之外的j, 满足Rj<Ri, 那么另外两个区间的选择余地必定不会减少 6 因此,为了使另外两个区间有尽可能多的选择,我们选择一个右端点最小的区间作为最左边的区间是最好的 7 同理,我们选择一个左端点最大的区间作为最右边的区间,也将提供最多的选择 8 确定了这两个区间之后,只需判断是否存在一个区间位于它们中间且不交叉即可 9

hdoj:2037

#include <iostream> using namespace std; struct Time { int start; int end; }; Time times[101]; int dp[101]; void sortTimes(Time times[], int n) { // 直接插入排序 for (int i = 1; i <= n; i++) { Time t = times[i]; int j = i - 1; while (j >= 1 &&am

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return