Simulation summary on the 16th

The kth integer
#include
using namespace std;
int n,a[20000],ans[40000] = {},b[20000] = {};
int main()
{ int h,k = 0; scanf("% d %d" ,&n,&h); for(int i = 1;i <= n; i++){ scanf("%d" ,&a[i]); } for(int i = 1;i <= n ; i++){ ans[a[i]]++; } for(int i = 1;i <= 39999; i++){ if(ans[i] != 0){ k++; //K++ in this place Put it inside, otherwise it will be meaningless b[k] = i;











	}
}
if(h <= k){
	printf("%d" ,b[h]);
}
else{
	printf("NO  RESULT");   //两个空格
}
return 0;

}
Scholarship
#include
#include
using namespace std;
struct stu{ int tot,id,chi,eng,mat; }st[10001]; bool compare(stu x,stu y){ if(x.tot != y.tot ) return x.tot> y.tot; if(x.chi != y.chi) return x.chi> y.chi; //Here we need to determine whether the languages ​​are equal, otherwise the id below will not take effect return x.id <y.id; } int n; int main() { scanf("%d" ,&n); for(int i = 1;i <= n; i++){ scanf("%d %d% d" ,&st[i].chi,&st[i].mat,&st[i].eng); st[i].id = i; st[i].tot = st[i].chi + st[ i].mat + st[i].eng; } sort(st + 1,st + 1 + n,compare); for(int i = 1;i <= 5; i++){


















printf("%d %d\n" ,st[i].id ,st[i].tot);
}
return 0;
}
Soldiers stand in line.
I think this is still greedy, just find the median
#include
#include
#include
using namespace std;
int main(){ int n; int a[20001] = {},b[20001] = {}; scanf("%d" ,&n); int place,toty = 0,totx = 0; for(int i = 1;i <= n; i++){ scanf("%d %d" ,&b[i],&a[i]); } sort(a + 1,a + 1 + n) ; if(n% 2 == 0) place = n / 2; else place = (n + 1) / 2; for(int i = 1;i <= n; i++){ toty = toty + abs(a[ i]-a[place]); } sort(b + 1,b + 1 + n); for(int i = 1;i <= n; i++){















b[i] = b[i]-i + 1; //x is not moved to a fixed point, so first preprocess x using rules to find the right midpoint
}
sort(b + 1,b + 1 + n );

for(int i = 1;i <= n; i++){
	totx = totx + abs(b[i] - b[place]); 
}
int tot = totx + toty;
printf("%d" ,tot);
return 0;

}
Statistics
#include
#include
using namespace std;
int n,biaoji = 0;
int a[200005]; //At first the array was reduced, and 4 data were directly stuck
int time;
int main()
{ scanf( "%d" ,&n); for(int i = 0;i <n; i++){ scanf("%d" ,&a[i]); } sort(a,a + n); time = 1; for (int i = 1;i <n; i++){ / if(a[i-1] != a[i]){ printf("%d %d\n" ,a[i-1],time) ; time = 1; } else{ time++; } } printf("%d %d" ,a[n-1] ,time); return 0; } Mode #include using namespace std; long long int n;






















long long int a[40000];
long long int ans[40000] = { };
long long int max = 0;
int main(){
scanf("%lld" ,&n);
for(int i = 1;i <= n; i++){
scanf("%lld" ,&a[i]);
}
for(int i = 1;i <= n; i++){
ans[a[i]]++;
}
for(int i = 1;i <= 30000; i++){ //桶排,最大的数有多大i就要循环到哪,别忘了
if(ans[i] > max){
max = ans[i];
}
}
for(int i = 1;i <= 30000; i++){
if(ans[i] == max){
printf("%d %d\n" ,i,max);
}
}
return 0;
}

Guess you like

Origin blog.csdn.net/Skywalker_____/article/details/112738337