求助求助,全re......

P1678 烦恼的高考志愿

C10UD @ 2024-02-20 21:45:29

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

long long m=0,n=0,k=10000000,sum=0;
long long a[10]={0},b[1000010]={0};
long long absolute(long long x)
{
    if(x>=0)return x;
    else return -x;
}
long long min(long long a,long long b)
{
    if(a<=b)return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long temp=*a;
    *a=*b;
    *b=temp;
    return;
}
void qqsort(long long begin,long long end)
{
    if(begin>=end)return;
    long long left=begin,right=begin+1;
    long long key=begin;
    while(right<=end)
    {
        if(a[right]<a[key])
        {
            left++;
            swap(&a[left],&a[right]);
        }
        right++;
    }
    swap(&a[left],&a[key]);
    qqsort(begin,left-1);
    qqsort(left+1,end);
    return;
}
void half(long long begin,long long end,long long key)
{
    if(begin>=end)return;
    long long mid=begin+(end-begin)/2;
    k=min(k,absolute(a[mid]-key));
    half(begin,mid,key);
    half(mid+1,end,key);
    return;
}
int main()
{
    scanf("%d%d",&m,&n);
    for(long long i=1;i<=m;i++)scanf("%lld",&a[i]);
    for(long long i=1;i<=n;i++)scanf("%lld",&b[i]);
    qqsort(1,m);
    for(long long i=1;i<=n;i++)
    {
        half(1,m,b[i]);
        sum+=k;
        k=10000000;
    }
    printf("%lld",sum);
    return 0;
}

by C10UD @ 2024-02-20 21:46:19

样例是过了的,但是不知道为什么全re,最开始还编译失败了


by QuQ_ @ 2024-02-21 20:46:56

@C10UD


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

long long m=0,n=0,k=10000000,sum=0;
long long a[100000]={0},b[10000010]={0};
long long absolute(long long x)
{
    if(x>=0)return x;
    else return -x;
}
long long min(long long a,long long b)
{
    if(a<=b)return a;
    else return b;
}
void swap(long long* a,long long* b)
{
    long long temp=*a;
    *a=*b;
    *b=temp;
    return;
}
void qqsort(long long begin,long long end)
{
    if(begin>=end)return;
    long long left=begin,right=begin+1;
    long long key=begin;
    while(right<=end)
    {
        if(a[right]<a[key])
        {
            left++;
            swap(&a[left],&a[right]);
        }
        right++;
    }
    swap(&a[left],&a[key]);
    qqsort(begin,left-1);
    qqsort(left+1,end);
    return;
}
void half(long long begin,long long end,long long key)
{
    if(begin>=end)return;
    long long mid=begin+(end-begin)/2;
    k=min(k,absolute(a[mid]-key));
    half(begin,mid,key);
    half(mid+1,end,key);
    return;
}
int main()
{
    scanf("%d%d",&m,&n);
    for(long long i=1;i<=m;i++)scanf("%lld",&a[i]);
    for(long long i=1;i<=n;i++)scanf("%lld",&b[i]);
    qqsort(1,m);
    for(long long i=1;i<=n;i++)
    {
        half(1,m,b[i]);
        sum+=k;
        k=10000000;
    }
    printf("%lld",sum);
    return 0;
}

by QuQ_ @ 2024-02-21 20:47:35

@C10UD RE解决了,但是TLE+WA+AC


by C10UD @ 2024-02-21 20:57:31

@QuQ_ 谢谢你!我再研究研究


|