计蒜客 烦恼的高考志愿题解

题目连接:https://nanti.jisuanke.com/t/T1874

题目

题面

计算机竞赛小组的蒜头君终于结束了万恶的高考,然而作为班长的他还不能闲下来,班主任给了他一个艰巨的任务:帮同学找出最合理的大学填报方案。可是蒜头君太忙了,于是他想到了同为计算机竞赛小组的你,请你帮他完成这个艰巨的任务。
根据 n 位学生的估分情况,分别给每位学生推荐一所学校,要求学校的预计分数线和学生的估分相差最小(可高可低,毕竟是估分嘛),这个最小值为不满意度。求所有学生不满意度和的最小值。

输入格式

在这里插入图片描述

输出格式

输出数据有一行,为最小的不满度之和。

样例

样例输入

4 3
513 598 567 689
500 600 550

样例输出

32

···································一条华丽的分割线···································

思路

就是二分!!!

标程

#include<bits/stdc++.h>
using namespace std;
int n,m,x[100001],y;
long long ans;
//	freopen("exam.in","r",stdin);
//	freopen("exam.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>x[i];
    sort(x+1,x+n+1);
    while(m--){
    
    
        cin>>y;
        int l=0,r=n+1;
        while(l<r){
    
    
            int mid=(l+r)>>1;
            if(x[mid]<=y)l=mid+1;
			else r=mid;
        }
        if(y<=x[1])ans+=x[1]-y;
        else ans+=min(abs(x[l-1]-y),abs(x[l]-y));
    }
    cout<<ans<<endl;
    return 0;
}

然而……
似乎一(亿)点问题

测评信息
================================================
测评用例 1:正确通过 [1.000 毫秒,824 KB]
---------------------------
测评用例 2:正确通过 [1.000 毫秒,824 KB]
---------------------------
测评用例 3:正确通过 [1.000 毫秒,824 KB]
---------------------------
测评用例 4:正确通过 [31.000 毫秒,824 KB]
---------------------------
测评用例 5:正确通过 [33.000 毫秒,824 KB]
---------------------------
测评用例 6:正确通过 [29.000 毫秒,824 KB]
---------------------------
测评用例 7:正确通过 [32.000 毫秒,824 KB]
---------------------------
测评用例 8:正确通过 [18.000 毫秒,824 KB]
---------------------------
测评用例 9:答案错误 [48.000 毫秒,824 KB]
用例输入:
100000 100000
79808 438278 497258 569994 60803 426031 660311 67857 808358 728753 839982 886334……
531664 661391 452414 199274 618734 170720 842030 385999 380027 680768 94886 5991……

用例正确输出:
500200

你的输出:
500173
---------------------------
测评用例 10:正确通过 [48.000 毫秒,824 KB]

结果
================================================
共 10 组测评用例,通过 9 组。

总分
================================================
9

这是那组没过的数据

扫描二维码关注公众号,回复: 11993728 查看本文章

真正的标程

#include<bits/stdc++.h>
using namespace std;
long long p1,p2,d1,d2,ans,x,num[1100000],n,m;
int main(){
    
    
//	freopen("exam.in","r",stdin);
//	freopen("exam.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
    cin>>m>>n;
    for(int i=0;i<m;i++)cin>>num[i];
    sort(num,num+m);
    while(n--){
    
    
        cin>>x;
        p1=lower_bound(num,num+m,x)-num;p2=p1-1;d1=d2=20000000;
        if(p1!=m)d1=num[p1]-x;
        if(p2!=-1)d2=x-num[p2];
        ans+=min(d1,d2);
    }
    cout<<ans<<endl;
    return 0;
}

完美AC

不要脸的求赞+关注+收藏

猜你喜欢

转载自blog.csdn.net/Richard_1101/article/details/109275888
今日推荐