Far Relative’s Problem(贪心算法)

来源:互联网 发布:我欲封天时装进阶数据 编辑:程序博客网 时间:2024/05/21 19:49

Far Relative’s Problem
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.

Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.

Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door’s friends.

Then follow n lines, that describe the friends. Each line starts with a capital letter ‘F’ for female friends and with a capital letter ‘M’ for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.

Output
Print the maximum number of people that may come to Famil Door’s party.

Sample Input

Input
4
M 151 307
F 343 352
F 117 145
M 24 128

Output
2

Input
6
M 128 130
F 128 131
F 131 140
F 131 141
M 131 200
M 140 200

Output
4

Hint
In the first sample, friends 3 and 4 can come on any day in range [117, 128].

In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.

此题数目较少,直接暴力。
想用一个数组存男人在L-R时间段里的人数(数组的数目表示今天能参加的男人数);同理将女人的存起来。
最后找到366天每一天男女参加人数的最小值的两倍,然后取出其中的最大值即可。
个人感觉好难,毕竟比较菜,有木有大神教教如何使用贪心??
代码如下:

#include <cstdio>#include <algorithm>#include<stdint.h>#include<math.h>#include<string.h>using namespace std;int a[2][400];int main(){    int n;    while(~scanf("%d",&n)){        memset(a,0,sizeof(a));    for(int i=1;i<=n;i++){        char s[3];int l,r;        scanf("%s%d%d",s,&l,&r);        if(s[0]=='M'){            for(int j=l;j<=r;j++)                a[0][j]++;        }        else{            for(int j=l;j<=r;j++)                a[1][j]++;        }    }    int t= 0;    for(int i=1;i<=366;i++)        t=max(t,2*min(a[0][i],a[1][i]));    printf("%d\n",t);    }    return 0;}
0 0
原创粉丝点击