Recently I came across this problem ,
A despotic king decided that his kingdom needed to be rid of corruption and disparity. He called his prime minister and ordered that all corrupt citizens be put to death. Moreover, he wanted this done quickly.
The wily prime minister realised that investigating every citizen to decide who was corrupt and who was not was rather difficult. So he decided on the following plan: He ordered all the citizens to appear in the court one by one and declare their wealth.
The king does not sit in the court all the time (he has other important business to attend to - for instance, meet dignitaries from neighbouring kingdoms, spend time with his family ...) Whenever the king walks into the court, the prime minister pulls out the richest man who has appeared before the court so far and is still alive and beheads him for being corrupt. Since the rich are more likely to be corrupt, he hopes to get rid of most of the corrupt and the king is happy as he sees his policy being implemented enthusiastically.
Suppose the wealth of the citizens trooping into the court is 1 3 7 6 5 18 9 11 2 4
and the king walked in three times: the first time after the first four persons have seen the minister, the second time after the first five persons have seen the minister and, finally after the first nine persons have seen the minister.
At the king's first visit the richest person to have met the minister has wealth 7 and he would be beheaded. At the second visit, the wealth of the richest person who has met the minister and is still alive has wealth 6 and so he would be beheaded. At the third visit the richest person to have met the minister who is still alive has wealth 18 and so he would be beheaded.
You may assume that the input is such that whenever the king walks in, it is always possible to behead someone.
Your aim is to write a program that will enable the prime minister to identify the richest man to have met the minister and who is still alive quickly. You may assume that no two citizens have the same wealth.
Input format
The first line of the input consists of two numbers N and M, where N is the number of citizens in the kingdom and M is the number of visits to the court by the king.
The next N+M lines describe the order in which the N citizens' appearances are interleaved with the M visits by the king. A citizen's visit is denoted by a positive integer, signifying his wealth. You may assume that no two citizens have the same wealth. A visit by the king is denoted by -1.
Output format
Your output should consist of M lines, where the ith line contains the wealth of the citizen who is beheaded at the ith visit of the king.
Test Data:
You may assume that M ≤ 10000 and N ≤ 100000. You may further assume that in 50% of the inputs M ≤ 1000 and N ≤ 8000.
My approach is like that ,until the king approaches store the numbers in an array , when the king approaches get the max number out of array , print it and make it 0 , the lowest value , but this is showing a TLE(Time Limit exceeded) for the last 3 testcases.
Do anyone have any better approach to this problem?
Here is the code:
#include <iostream>
#include <vector>
#include <algorithm>
int findGreatest(std::vector<int>vec){
int n = vec.size();
int arr[n][n];
for(int i=0;i<n;i++){
arr[n][i] = vec[i];
}
for(int i=n-2;i>=0;i--){
for(int j=0;j<n-1;j++){
if(arr[i+1][j] > arr[i+1][j+1]){
arr[i][j] = arr[i+1][j];
}else{
arr[i][j] = arr[i+1][j+1];
}
}
}
return arr[0][0];
}
int getPos(std::vector<int> vec, int num){
for(int i=0;i < vec.size();i++){
if(num == vec[i]){
return i;
break;
}
}
}
int main (int argc, char const* argv[])
{
int n , k ,i=0;
std::cin >> n >> k;
std::vector<int>riches , visits;
while(k){
int a;
std::cin >> a;
if(a == -1){
k--;
int b = getPos(riches,findGreatest(riches));
std::cout << b << findGreatest(riches) << std::endl;
std::cout << riches[b] << std::endl;
riches[b] = 0;
}else{
riches.push_back(a);
}
i++;
}
return 0;
}
Edit : So many views for a solved question , well if possible can anyone take a look at this and this queries?
So many views for a solved question
CR isn't about problem solving, but doing it in style. \$\endgroup\$