7 solutions

  • 0
    @ 2022-1-3 19:16:09

    原题链接

    #include<stdio.h>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int a[100100],b[100100];
    int find(int n,int m)
    {
    	int ans=0;
    	for(int i=0;i<m;i++)
    	{
    		int l=0,r=n-1;
    	    while(l<r)
    	    {
    		    int mid=l+r>>1;
    		    if(a[mid]<=b[i]) l=mid+1;
    		    else r=mid;
    	    }
    	    if(b[i]<=a[0])
    	    {
    	    	ans+=a[0]-b[i];
    		}
    		else
    		{
    			ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
    		}
    	}
    	printf("%d",ans);
    }
    int main()
    {
    	int n,m;
    	scanf("%d%d",&n,&m);
    	for(int i=0;i<n;i++)
    	{
    		scanf("%d",&a[i]);
    	}
    	for(int i=0;i<m;i++)
    	{
    		scanf("%d",&b[i]);
    	}
    	sort(a,a+n);
    	find(n,m);
    	return 0;
    }
    
    

    Information

    ID
    280
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    266
    Accepted
    51
    Uploaded By