結果

問題 No.130 XOR Minimax
ユーザー akakimidoriakakimidori
提出日時 2017-06-18 07:12:25
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 372 ms / 5,000 ms
コード長 1,352 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 26,260 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-10 00:18:51
合計ジャッジ時間 4,755 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
4,352 KB
testcase_01 AC 0 ms
4,348 KB
testcase_02 AC 0 ms
4,348 KB
testcase_03 AC 0 ms
4,352 KB
testcase_04 AC 10 ms
4,352 KB
testcase_05 AC 15 ms
4,348 KB
testcase_06 AC 169 ms
4,348 KB
testcase_07 AC 167 ms
4,352 KB
testcase_08 AC 372 ms
4,348 KB
testcase_09 AC 20 ms
4,348 KB
testcase_10 AC 36 ms
4,352 KB
testcase_11 AC 138 ms
4,352 KB
testcase_12 AC 7 ms
4,348 KB
testcase_13 AC 114 ms
4,348 KB
testcase_14 AC 332 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 185 ms
4,376 KB
testcase_17 AC 198 ms
4,352 KB
testcase_18 AC 220 ms
4,352 KB
testcase_19 AC 295 ms
4,348 KB
testcase_20 AC 102 ms
4,352 KB
testcase_21 AC 336 ms
4,348 KB
testcase_22 AC 28 ms
4,352 KB
testcase_23 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define MIN(a,b) ((a)<(b)?(a):(b))

void msort(int *a,int n,int *merge){
  if(n<=16){
    int i,j;
    for(i=1;i<n;i++){
      int p=a[i];
      j=i-1;
      while(j>=0 && p<a[j]){
	a[j+1]=a[j];
	j--;
      }
      a[j+1]=p;
    }
    return;
  }

  int m=n/2;
  msort(a,m,merge);
  msort(a+m,n-m,merge);

  int i,j,k;
  i=0;
  j=m;
  k=0;
  while(i<m && j<n){
    if(a[i]<=a[j]){
      merge[k++]=a[i++];
    } else {
      merge[k++]=a[j++];
    }
  }
  while(i<m) merge[k++]=a[i++];
  while(j<n) merge[k++]=a[j++];

  for(i=0;i<n;i++) a[i]=merge[i];
  return;
}

int calc(int *array,int n,int shift){
  if(n<=1) return 0;
  if(shift==0) return (array[0]&0x01)!=(array[n-1]&0x01);
  if(((array[0]>>shift)&0x01) == ((array[n-1]>>shift)&0x01)) return calc(array,n,shift-1);

  int l=0;
  int r=n;
  while(l+1<r){
    int m=(l+r)/2;
    if((array[m]>>shift)&0x01){
      r=m;
    } else {
      l=m;
    }
  }
  return (1<<shift)+MIN(calc(array,r,shift-1),calc(array+r,n-r,shift-1));
}

void run(void){
  int n;
  scanf("%d",&n);
  int *array=(int *)malloc(sizeof(int)*n);
  int i;
  for(i=0;i<n;i++){
    scanf("%d",array+i);
  }
  int *merge=(int *)malloc(sizeof(int)*n);
  msort(array,n,merge);
  free(merge);
  int ans=calc(array,n,30);
  printf("%d\n",ans);
  return;
}

int main(void){
  run();
  return 0;
}
0