Scoring in Exam

QUESTION

Milly is at the examination hall where she is reading a question paper. She checked the question paper and discovered that there are N questions in that paper. Each question has some score value. Ideally it’s like questions requiring more time have more score value and strangely no two questions on the paper require same time to be solved.\n\nShe is very excited by looking these questions. She decided to solve K questions while maximizing their score value. Could you please help Milly to determine the exact time she needs to solve the questions.\n\nInput\n\nFirst line of input contains two space separated integers N and Q, where N is the number of questions available and Q is number of queries\nNext line contains N space separated integers denoting the time Ti of N questions\nNext line contains N space separated integers denoting the scores Si of N questions\nNext Q lines contains a number K each, the number of questions she wants to solve\nOutput\n\nPrint the time required for each query in a separate line.\nConstraints\n\n1 <= N <= 105 \n1 <= Q <= 105 \n1 <= K <= N \n1 <= Ti, Si <= 109.

“TESTCASE_1”: “5 2\n2 3 9 4 5\n3 5 11 6 7\n5\n3\n###—###SEPERATOR—###—\n23\n18”, “TESTCASE_2”: “6 4\n3 5 1 7 4 2\n5 7 3 9 6 4\n5 \n2\n4\n6\n###—###SEPERATOR—###—\n21\n12\n19\n22”, “TESTCASE_3”: “10 10\n3 5 1 7 4 2 21 15 99 6\n5 7 3 9 6 4 23 17 101 8\n5 \n2\n4\n6\n7\n3\n10\n9\n8\n1\n###—###SEPERATOR—###—\n148\n120\n142\n153\n157\n135\n163\n162\n160\n99”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
 
struct data{
	int Time;
	int Score;
};
 
int compare(struct data *e1,struct data *e2){
	if(e1->Score<e2->Score)
		return -1;
	else if(e1->Score > e2->Score)
		return 1;
	else
		return 0;
}
 
int main()
{
    int N,Q,i,input_val;
    struct data array[100000];
    scanf("%d%d",&N,&Q);
    for(i=0;i<N;i++)
    scanf("%d",&array[i].Time);
    for(i=0;i<N;i++)
    scanf("%d",&array[i].Score);
    qsort(array,N,sizeof(struct data),compare);
    for(i=N-1;i>0;i--)
    	array[i-1].Time =array[i-1].Time + array[i].Time;
    for(i=0;i<Q;i++){
    	scanf("%d",&input_val);
    	printf("%d\n",array[N-input_val].Time);
    }
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO