首页 > 代码库 > Electronic Auction
Electronic Auction
Electronic Auction
Time limit: 0.5 second
Memory limit: 64 MB
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.
Output
Output the profit of the auction in the current month with 2 digits after the decimal point.
Sample
input | output |
---|---|
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。