B - Median-----(2015 summer training #8(Qualifying))
2015-08-14 13:03
495 查看
B - Median
时限:5000MS 内存:65536KB 64位IO格式:%lld
& %llu
问题描述
The median of m numbers is after sorting them in order, the middle one number of them if m is even or the average number of the middle 2 numbers if m is
odd. You have an empty number list at first. Then you can add or remove some number from the list.
For each add or remove operation, output the median of the number in the list please.
输入
This problem has several test cases. The first line of the input is an integer T (0<T<=100) indicates the number of test cases. The first line of each
test case is an integer n (0<n<=10000) indicates the number of operations. Each of the next n lines is either "add x" or "remove x"(-231<=x<231)
indicates the operation.
输出
For each operation of one test case: If the operation is add output the median after adding x in a single line. If the operation is removeand
the number x is not in the list, output "Wrong!" in a single line. If the operation is remove and the number x is in the list, output the median after deleting x in a single
line, however the list is empty output "Empty!".
样例输入
样例输出
if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s.
分析:看了这道题,学习了容器multiset,收获还是蛮多的。然而并没有AC。
CODE:
时限:5000MS 内存:65536KB 64位IO格式:%lld
& %llu
问题描述
The median of m numbers is after sorting them in order, the middle one number of them if m is even or the average number of the middle 2 numbers if m is
odd. You have an empty number list at first. Then you can add or remove some number from the list.
For each add or remove operation, output the median of the number in the list please.
输入
This problem has several test cases. The first line of the input is an integer T (0<T<=100) indicates the number of test cases. The first line of each
test case is an integer n (0<n<=10000) indicates the number of operations. Each of the next n lines is either "add x" or "remove x"(-231<=x<231)
indicates the operation.
输出
For each operation of one test case: If the operation is add output the median after adding x in a single line. If the operation is removeand
the number x is not in the list, output "Wrong!" in a single line. If the operation is remove and the number x is in the list, output the median after deleting x in a single
line, however the list is empty output "Empty!".
样例输入
2 7 remove 1 add 1 add 2 add 1 remove 1 remove 2 remove 1 3 add -2 remove -2 add -1
样例输出
Wrong! 1 1.5 1 1.5 1 Empty! -2 Empty! -1
Hint
if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s.分析:看了这道题,学习了容器multiset,收获还是蛮多的。然而并没有AC。
CODE:
#include <iostream> #include <string.h> #include <set> #include <cstdio> using namespace std; int main() { int t; cin>>t; while(t--){ int n; // cin>>n; scanf("%d",&n); // string s; char s[10]; int num; multiset<int> arr; multiset<int>::iterator it; it=arr.begin(); bool vis=false; while(n--){ // cin>>s>>num; scanf("%s",s); scanf("%d",&num); if(!strcmp(s,"remove")){ int len=arr.size(); multiset<int>::iterator itt; itt=arr.find(num); if(*it==num){ itt=it; if(arr.size()&1) it++; else it--; } if(itt!=arr.end()) arr.erase(itt); else{ cout<<"Wrong!"<<endl; continue; } if(arr.empty()){ cout<<"Empty!"<<endl; continue; } } else{ arr.insert(num); if(!vis){ it=arr.begin(); vis=true; } else if((arr.size()&1)&&num<*it) it--; else if(arr.size()%2==0&&num>*it) it++; } int len=arr.size(); if(len&1) // cout<<*it<<endl; printf("%d\n",*it); else{ multiset<int>::iterator tmp; tmp=it; int a=*it,b=*(--tmp); long long ans=(a+b)/2.0; // cout<<ans<<endl; if((a+b)%2==0) printf("%lld\n",ans); else if(a+b==-1) printf("-0.5\n"); else printf("%lld.5\n",ans); } } memset(s,0,sizeof(s)); // arr.~multiset(); arr.clear(); } return 0; }
相关文章推荐
- Could not connect to '10.7.100.182' (port 22): Connection failed
- HIT 2060 Fibonacci Problem Again
- LeetCode Factorial Trailing Zeroes
- LeetCode Factorial Trailing Zeroes
- epoll_create, epoll_ctl和epoll_wait
- 2015 Multi-University Training Contest 2(hdu 5300 - hdu 5309)
- Clock Skew , Clock Uncertainty和 Period
- libvirtError: no connection driver available for qemu:///system 解决办法
- 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa
- tools:context=".MainActivity的作用
- 使用Grails快速开发Web应用程序
- 构建您的第一个Grails 应用程序
- 2015 Multi-University Training Contest 1(hdu 5288 - hdu 5299)
- hdu1021Fibonacci Again找规律
- PS和AI软件区别
- HDU - 3394 Railway(连通分量+环)
- qmail 相关问题
- 智能改变未来TurboMail邮件系统招商新计划
- UVA 10986 Sending email 【dijkstra + 堆优化】
- POJ 1363 Rails