CSP 202012-2 期末预测之最佳阈值 C语言满分答案
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* _a , const void* _b)
{
int* a = (int*)_a;
int* b = (int*)_b;
return *a - *b;
}
main()
{
int m;
int i,j,k;
int out=0,best_count=0;
int arr_every_count=0;
int tmp = 0,how_continuity=0;
scanf("%d",&m);
int arr_everysmaller_count[m];
int arr_everybigger_count[m];
int arr[m][2];
for(i=0;i<m;i++) scanf("%d %d",&arr[i][0],&arr[i][1]);
qsort(arr,m,sizeof(arr[0]),cmp);
arr_every_count = 0;
for(i=m-1;i>=0;i--)
{
if(arr[i][1]==1)arr_every_count++;
arr_everybigger_count[i] = arr_every_count;
if(tmp==arr[i][0])
{
how_continuity++;
}
if(how_continuity>0&&tmp!=arr[i][0])
{
for(j=i+1;j<=i+how_continuity;j++)
{
arr_everybigger_count[j] = arr_everybigger_count[i+1];
}
how_continuity=0;
}
tmp = arr[i][0];
}
arr_every_count = 0;
tmp = 0;
for(i=0;i<m;i++)
{
arr_everysmaller_count[i] = arr_every_count;
if(arr[i][1]==0)arr_every_count++;
if(tmp==arr[i][0])
{
how_continuity++;
}
if(how_continuity>0&&tmp!=arr[i][0])
{
for(j=i-1;j>=i-how_continuity-1;j--)
{
arr_everysmaller_count[j] = arr_everysmaller_count[i-how_continuity-1];
}
how_continuity=0;
}
tmp = arr[i][0];
}
for(i=0;i<m;i++)
{
int count = arr_everysmaller_count[i] + arr_everybigger_count[i];
if(count>=best_count)
{
if(count==best_count)
{
if(out<arr[i][0])
{
out = arr[i][0];
}
}else
{
out = arr[i][0];
best_count = count;
}
}
}
printf("%d",out);
return 0;
}
|