Scientific Conference URAL - 1203[贪心 || DP]

1203. Scientific Conference

Time limit: 1.0 second
Memory limit: 64 MB
Functioning of a scientific conference is usually divided into several simultaneous sections. For example, there may be a section on parallel computing, a section on visualization, a section on data compression, and so on.
Obviously, simultaneous work of several sections is necessary in order to reduce the time for scientific program of the conference and to have more time for the banquet, tea-drinking, and informal discussions. However, it is possible that interesting reports are given simultaneously at different sections.
A participant has written out the time-table of all the reports which are interesting for him. He asks you to determine the maximal number of reports he will be able to attend.

Input

The first line contains the number 1 ≤  N ≤ 100000 of interesting reports. Each of the next  N lines contains two integers  Tsand  Te separated with a space (1 ≤  Ts <  Te ≤ 30000). These numbers are the times a corresponding report starts and ends. Time is measured in minutes from the beginning of the conference.

Output

You should output the maximal number of reports which the participant can attend. The participant can attend no two reports simultaneously and any two reports he attends must be separated by at least one minute. For example, if a report ends at 15, the next report which can be attended must begin at 16 or later.

Sample

inputoutput
5
3 4
1 5
6 7
4 5
1 3
3
Problem Author: Magaz Asanov
Problem Source: USU Internal Contest, March 2002

© 2000–2017 Timus Online Judge Team. All rights reserved.

解法一(贪心):贪心非常好做,只需要按照结束时间排序即可。。。


#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e5+7;
typedef struct node{
    int ts, te;
} ND;

ND m[maxn];
int n;

bool cmp(ND a, ND b)
{
    return a.te < b.te;
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i++) scanf("%d%d",&m[i].ts,&m[i].te);
        sort(m, m+n, cmp);
        int te = 0;
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            if(m[i].ts >= te) {
                ans++;
                te = m[i].te + 1;
            }
        }
        printf("%d", ans);
    }

    return 0;
}


解法二(DP) :

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e5+7;
typedef struct node{
    int ts, te;
} ND;

ND m[maxn];
int n;
int dp[maxn];
int k[maxn];

bool cmp(ND a, ND b)
{
    if(a.te == b.te) return a.ts < b.ts;
    return a.te < b.te;
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d",&n))
    {
        memset(dp, 0, sizeof(dp));
        memset(k, 0, sizeof(k));
        int last = -1;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&m[i].ts,&m[i].te);
            last = max(last, m[i].te);
        }
        sort(m, m+n, cmp);
        for(int i = 0; i < n; i++)
        {
            dp[m[i].te] = 1;   //记录结束时间,有一个活动
            k[m[i].te] = m[i].ts;   //记录结束的活动对应的开始时间;
        }                            //因为前面的排序,所以开始早的会被覆盖,是最优的;
        for(int i = 0; i <= last; i++)
        {
            if(k[i]) dp[i] = max(dp[i - 1], dp[k[i] - 1] + 1);
            else dp[i] = max(dp[i], dp[i - 1]);
        }
        printf("%d", dp[last]);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值