首页 > 代码库 > Electronic Auction

Electronic Auction

Electronic Auction

Time limit: 0.5 second
Memory limit: 64 MB
There is a deficit in cast-iron pigs in the country. They are sold at an electronic auction. Customers make their bids: announce a price at which they are ready to buy a pig. From time to time a seller puts up for sale К pigs at a price of X bibriks each. The first К customers who offered the same or higher price get one pig each.
Customers may cancel their bids (after a purchase a bid remains valid until it is canceled). Only bids made in a current month are valid, so each month a customer should renew his bid. If a seller did not sell all the pigs offered for sale, then the unsold pigs remain at his storehouse and don’t participate in the auction any more.
Each sold cast-iron pig makes a profit of 0.01 bibriks for the auction. Having a month‘s log of auction operations, you are to calculate the profit of the auction in this month.

Input

The input contains a month‘s operations log, one operation per line. There are three types of operations:
  • “BID X” — a customer announces that he is ready to buy a pig at a price of X bibriks;
  • “DEL X” — a customer cancels his bid for a pig at a price of X bibriks;
  • “SALE X K” — a seller puts up for sale К pigs at a price of X bibriks.
X is between 0.01 and 10000.00 bibriks and has at most 2 digits after the decimal point. K is an integer between 1 and 100000. The number of operations does not exceed 100000. All operations are correct. The last line contains the word “QUIT”.

Output

Output the profit of the auction in the current month with 2 digits after the decimal point.

Sample

inputoutput
BID 0.01BID 10000BID 5000BID 5000SALE 7000 3DEL 5000SALE 3000 3SALE 0.01 3QUIT
0.06

 

分析:离散化+树状数组;

代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <hash_map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=1e5+10;const int dis[][2]={0,1,-1,0,0,-1,1,0};using namespace std;using namespace __gnu_cxx;ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}int n,m,k,t,a[maxn],num;double c[maxn];ll ans;struct node{    char a[10];    double b;    int c;}op[maxn];void add(int x,int y){    for(int i=x;i<=num;i+=(i&(-i)))    {        a[i]+=y;    }}int get(int x){    int ans=0;    for(int i=x;i;i-=(i&(-i)))        ans+=a[i];    return ans;}int main(){    int i,j;    while(~scanf("%s",op[++n].a)&&op[n].a[0]!=Q)    {        scanf("%lf",&op[n].b);        c[n]=op[n].b;        if(op[n].a[0]==S)scanf("%d",&op[n].c);    }    n--;    sort(c+1,c+n+1);    num=unique(c+1,c+n+1)-c-1;    rep(i,1,n)op[i].b=lower_bound(c+1,c+num+1,op[i].b)-c;    rep(i,1,n)    {        if(op[i].a[0]==B)add((int)op[i].b,1);        else if(op[i].a[0]==D)add((int)op[i].b,-1);        else ans+=min(op[i].c,get(num)-get((int)op[i].b-1));    }    printf("%.2f\n",(double)ans/100);    //system("Pause");    return 0;}

Electronic Auction