Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.
Vasya has a train lighting schedule for all n hours of the trip — n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.
The first input line contains two integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ n) — the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≤ ai ≤ 100), ai is the light level at the i-th hour.
In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, — the indexes of hours Vasya will read at (1 ≤ bi ≤ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers biin an arbitrary order.
5 3 20 10 30 40 10
20 1 3 4
6 5 90 20 35 40 60 100
35 1 3 4 5 6
In the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.
题:
输出第n大的数,以及排序后前n个大的数的下标。
一开始的想法:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<fstream>
using namespace std;
int find_max_now(int light[],int n);
int n,k;
int light[1005],visit[1005];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d%d",&n,&k);
memset(visit,0,sizeof(visit));
for(int i=0;i<n;i++)scanf("%d",&light[i]);
int low;
for(int i=0;i<k;i++)
{
low=find_max_now(light,n);//第n大的数
}
printf("%d\n",low);
for(int i=0;i<n;i++)
if(visit[i]==1)
printf("%d ",i+1);
printf("\n");
}
int find_max_now(int light[],int n)
{//函数作用找到未被标记过的当前数列中最大值,执行n次就是找到第n大的数
int maxn_now=-1,pos=-1;
for(int i=0;i<n;i++)
if(light[i]>maxn_now&&visit[i]==0){
maxn_now=light[i];
pos=i;
}
visit[pos]=1;
return maxn_now;
}
/*简单的从数组中从大到小挑选k个数字的问题*/
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
int main()
{
pair<int,int>light[1005];
int n,k,i;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>light[i].first;
light[i].second=i+1;
}
sort(light,light+n);
cout<<light[n-k].first<<endl;
for(i=n-k;i<n;i++)
{
cout<<light[i].second<<' ';
}
cout<<endl;
return 0;
}
/*注意点:
题目要求文件读取,pair(),sort的用法,pair()定义在<utility>中,但pair()在std中已声明,可不写<utility>头文件
注意用sort对pair()进行排序时依据.first排序,C++ endl可视作换行符*/