Online minimization problem
Description
To a space-separated string, the string only four characters, respectively:
(1) space, representing the separator;
(2) an integer value representing the currently inserted;
(3) the letter E, represents from previously removal of a minimum digital value;
(4) the decimal point indicates the end of input. "";
known insert has n, m one operand removed, find the number of m sequentially acquired, taken to ensure that each input It has a number of desirable hours. (Between 1≤m≤n≤100000 each number in the range -10000 and 10000)
Input
Only his string
Output
Only one row of data, separated by spaces of m numbers, respectively, the minimum value of m sequentially obtained.
Sample Input
4 8 E 3 E 10 2 6 E 1.
Sample Output
4 3 2
HINT
n <= 200, all the input data are <= 1000, is less than the determined minimum of 10 ^ 9.
Source
#include<bits/stdc++.h>
using namespace std;
int s[100001],len;
void s_up(int p)
{
while(p>1&&s[p/2]>s[p])
{
swap(s[p/2],s[p]);
p=p/2;
}
return;
}
void s_down(int p)
{
int lt;
while(1)
{
if(p*2>len) return;
if(p*2==len) lt=p*2;
else
{
if(s[p*2]<s[p*2+1])
lt=p*2;
else
lt=p*2+1;
}
if(s[p]>s[lt])
{
swap(s[p],s[lt]);
p=lt;
}
else break;
}
return;
}
void insert(int key)
{
len++;
s[len]=key;
s_up(len);
}
int main()
{
char c;
int num=0;
bool flag=0;
int fh=1;
while(1){
c=getchar();
if(c=='.') break;
else if(c=='E')
{
printf("%d ",s[1]);
s[1]=s[len--];
s_down(1);
}
else if(c==' ')
{
if(flag==1)
{
insert(fh*num);
num=0;
flag=0;
fh=1;
}
}
else
if(c=='-') fh=-1;
else{
flag=1;
num=num*10+c-'0';
}
}
return 0;
}