View Code of Problem 5

#include<stdio.h>
int main(){
	int i, t;
	scanf("%d",&t);
	for(i = 0; i < t; i ++){
		int j,k,m,n;
		scanf("%d%d",&n,&m);
		int fruit[n][2], sort[n];
		for(j = 0; j < n; j ++){
			for(k = 0; k < 2; k ++){
				scanf("%d",&fruit[j][k]);
			}
		}
		for(j = 0; j < n; j ++){
			sort[j] = j;
		}
		for(j = 0; j < n - 1; j ++){
			for(k = 0; k < n - 1 - j; k ++){
				if(fruit[k][0] == fruit[k + 1][0]){
					if(fruit[k][1] >= fruit[k + 1][1]){
						int temp = fruit[k][0];
						fruit[k][0] = fruit[k + 1][0];
						fruit[k + 1][0] = temp;
						temp = fruit[i][1];
						fruit[k][1] = fruit[k + 1][1];
						fruit[k + 1][1] = temp;
						temp = sort[k];
						sort[k] = sort[k + 1];
						sort[k + 1] = temp;
					}
				} else if(fruit[k][0] >= fruit[k + 1][0]){
					int temp = fruit[k][0];
					fruit[k][0] = fruit[k + 1][0];
					fruit[k + 1][0] = temp;
					temp = fruit[k][1];
					fruit[k][1] = fruit[k + 1][1];
					fruit[k + 1][1] = temp;
					temp = sort[k];
					sort[k] = sort[k + 1];
					sort[k + 1] = temp;
				}
			}
		}
		for(j = n - 1; j > n - m - 1; j --){
			if(j != n - m){
				printf("%d ",sort[j] + 1);
			} else {
				printf("%d\n",sort[j] + 1);
			}
		}
		
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 5