想不通哪里有问题
  • 板块P1007 独木桥
  • 楼主miny1233
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/10/19 22:55
  • 上次更新2023/11/4 03:13:16
查看原帖
想不通哪里有问题
582818
miny1233楼主2021/10/19 22:55
#include<stdio.h>
void _min(int*,int*,int,int,int);
void _max(int*,int*,int,int,int);
void BubbleSort(int *arr, int size)  
{  
    int i, j, tmp;  
    for (i = 0; i < size - 1; i++) {  
        for (j = 0; j < size - i - 1; j++) {  
            if (arr[j] > arr[j+1]) {  
                tmp = arr[j];  
                arr[j] = arr[j+1];  
                arr[j+1] = tmp;  
            }  
        }  
    }  
} 
int main(){
    int the_longgest;
    int _long,sol,n;
    scanf("%d",&_long);
    scanf("%d",&sol);
    int pos[sol];
    for(n=0;n<sol;n++){
        scanf("%d",&pos[n]);
    }
    if(_long%2==0){
        the_longgest=_long/2;
    }
    else{
        the_longgest=(_long-1)/2+1;
    }
    int left[sol],right[sol],v,l,r;
    for(n=0;n<sol;n++){
        v = pos[n]-the_longgest;
        if(v>0){
            right[++r]=v;
        }else{
            left[++l]=-v;
        }
    }
    BubbleSort((int*)left,l);
    BubbleSort((int*)right,r);
    _min((int*)left,(int*)right,l,r,the_longgest);
    _max((int*)left,(int*)right,l,r,the_longgest);
    return 0;
}
void _min(int *left,int*right,int l,int r,int the_loggest){
    printf("%d\n",*left<*right?the_loggest-*left:the_loggest-*right);
    return;
}
void _max(int *left,int*right,int l,int r,int the_loggest){
    int w,v,hl,hr;
    while(1){
        v = abs(*left-*right);
        if(v==1||v==0){
            printf("%d\n",*left<*right?the_loggest+*left+w:the_loggest+*right+w);
            return;
        }
        if(*left>0&&!hl){
            *left--;
        }else{
            hl=1;
            *left++;
        }
        if(*right>0&&!hr){
            *right--;
        }else{
            hr=1;
            *right++;
        }
        w++;
    }
}
2021/10/19 22:55
加载中...