各位大佬看看改成long long后还是不能通过Subtask #1

P1678 烦恼的高考志愿

yayayalll @ 2023-12-29 23:21:01

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int main(){
    long long m,n;
    long long arr[100010],num[100010],l,r,mid,count=0;
    cin>>m>>n;
    for(int i=0;i<m;i++){
        cin>>arr[i];
    }
    for(int i=0;i<n;i++){
        cin>>num[i];
    }
    sort(arr,arr+m);
    for(int i=0;i<n;i++){
        l=0,r=m-1;
        while(l<r){
            mid=(l+r+1)/2;
            if(arr[mid]<=num[i])
            l=mid;
            else
            r=mid-1;
        }
        count+=min(abs(arr[l]-num[i]),abs(arr[l+1]-num[i]));    
    }
    cout<<count<<endl;  
}

by danlao @ 2023-12-30 08:58:09

以我的二分来看,你应该是l=mid+1r=mid


by yayayalll @ 2023-12-31 14:01:30

@yaodiguoan 改成你这样直接全部tle了


by danlao @ 2023-12-31 14:47:13

哦?那我再看看


by danlao @ 2023-12-31 14:47:59

#include <bits/stdc++.h>
using namespace std;
long long a[100100],b[100100];
int main(){
    long long n,m;
    cin>>n>>m;
    for(long long i=1; i<=n; i++){
        cin>>a[i];
    }
    for(long long i=1; i<=m; i++){
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    long long ans=0;
    for(long long i=1; i<=m; i++){
        if(b[i]<=a[1]){
            ans+=a[1]-b[i];
        }
        else{
            long long l=0,r=n+1;
            while(l<r){
                long long mid=(l+r)/2;
                if(a[mid]<=b[i]){
                    l=mid+1;
                }
                else{
                    r=mid;
                }
            }
            ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
        }
    }
    cout<<ans;
    return 0;
}

这是我的代码,你可以多照一下


by danlao @ 2023-12-31 14:48:39

等,你那应该是mid=(l+r)/2!


by qusia_MC @ 2024-01-01 20:25:05

你这个的话,应该少一个特判m=1;这样的话如果m=1的话就直接ans+=abs(a[1]-b) 就完事了 粘上代码::

include<bits/stdc++.h>

using namespace std; int main() { long long m,n,ans=0; cin>>m>>n; long long a[m+1],b; for( long long i=1;i<=m;i++)cin>>a[i]; sort(a+1,a+m+1); while(n--) { cin>>b; long long l=1,r=m; if(m==1){ ans+=abs(a[1]-b); } else { while(l<r) { long long cmt=(l+r)/2; if(a[cmt]<=b)l=cmt+1; else r=cmt; } if(b<=a[1])ans+=a[1]-b; else ans+=min(abs(a[l-1]-b),abs(a[l]-b)); } } cout<<ans; return 0; }


by qusia_MC @ 2024-01-01 20:25:37

你这个的话,应该少一个特判m=1;这样的话如果m=1的话就直接ans+=abs(a[1]-b) 就完事了 粘上代码::

#include<bits/stdc++.h>
using namespace std;
int main()
{
     long long m,n,ans=0;
    cin>>m>>n;
    long long a[m+1],b;
    for( long long i=1;i<=m;i++)cin>>a[i];
    sort(a+1,a+m+1);
    while(n--)
    {
        cin>>b;
         long long l=1,r=m;
        if(m==1){
            ans+=abs(a[1]-b);
        }
        else
        {
           while(l<r)
        {
            long long cmt=(l+r)/2;
           if(a[cmt]<=b)l=cmt+1;
           else r=cmt;
        }
        if(b<=a[1])ans+=a[1]-b;
        else ans+=min(abs(a[l-1]-b),abs(a[l]-b));
        }
    }
    cout<<ans;
    return 0;
}

by qusia_MC @ 2024-01-01 20:26:13

上面那个粘的不好,看下面这个


by qiuhuiyao @ 2024-01-04 20:59:47

@William2019 望丰展?使用mark或lat公式


|