CodeForces - 222D:Olympiad(STL,贪心)

11 篇文章 0 订阅
10 篇文章 0 订阅

Discription
A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya’s chances.

Help Vasya’s teacher, find two numbers — the best and the worst place Vasya could have won. Note that the total results’ table sorts the participants by the sum of points for both tours (the first place has the participant who has got the most points). If two or more participants have got the same number of points, it’s up to the jury to assign places to them according to their choice. It is guaranteed that each participant of the Olympiad participated in both tours of the Olympiad.

Input
The first line contains two space-separated integers n, x (1 ≤ n ≤ 105; 0 ≤ x ≤ 2·105) — the number of Olympiad participants and the minimum number of points Vasya earned.

The second line contains n space-separated integers: a1, a2, …, an (0 ≤ ai ≤ 105) — the participants’ points in the first tour.

The third line contains n space-separated integers: b1, b2, …, bn (0 ≤ bi ≤ 105) — the participants’ points in the second tour.

The participants’ points are given in the arbitrary order. It is guaranteed that Vasya was present in the Olympiad — there are two integers i, j (1 ≤ i, j ≤ n) such, that ai + bj ≥ x.

Output
Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad.

Examples
Input
5 2
1 1 1 1 1
1 1 1 1 1
Output
1 5
Input
6 7
4 3 5 6 4 4
8 6 0 4 3 4
Output
1 5

Note
In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury’s decision, Vasya can get the first (the best) as well as the last (the worst) fifth place.

In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the total results’ table looks like that — {4:8, 6:4, 3:6, 4:4, 4:3, 5:0}.

In this table all participants are sorted by decreasing points and we can see how much a participant earned in the first and in the second tour.

In the worst case scenario Vasya can get the fifth place if the table looks like that — {4:8, 4:6, 6:4, 5:4, 4:3, 3:0}, and he earned 4 and 3 points in the first and second tours, correspondingly.

题意
给定一个人两场比赛的最小总分k,和n个人两场比赛的分数(随即排列),问这个人最高和最低的排名是多少。

思路
因为给定的是这个人最小分数,即他的分数可以是无穷大,即最高为第一名。只需要找到最低排名就可。
将第一场比赛的全部分数a[i]都放在multiset中,输入第二场比赛分数b[i]时在multiset中查找是否有k-b[i],有则加一,输出结果。

AC代码

#include<bits/stdc++.h>
using namespace std;
int n,k,sum;
int a[100100],b[100100];
multiset<int>v;
multiset<int>::iterator it;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v.insert(a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        it=v.lower_bound(k-b[i]);
        if(it!=v.end())
        {
            v.erase(it);
            sum++;
        }
    }
    cout<<1<<" "<<sum<<endl;
}

老谈代码:
她是用指针同时从前后遍历判断的方法,时间复杂度相同。

#include<bits/stdc++.h>
using namespace std;
int a[100010],b[100010];
int main()
{
    int n,x;
    cin>>n>>x;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
        cin>>b[i];
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    int ans=0;
    for(int i=1,j=n;i<=n&&j>=1;i++)
    {
        if(a[i]+b[j]>=x)
        {
            ans++;
            j--;
        }
    }
    cout<<1<<" "<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值