B - Median-----(2015 summer training #8(Qualifying))
来源:互联网 发布:kindle免费资源 知乎 编辑:程序博客网 时间:2024/05/17 04:42
问题描述
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!".
样例输入
27remove 1add 1add 2add 1remove 1remove 2remove 13add -2remove -2add -1
样例输出
Wrong!11.511.51Empty!-2Empty!-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;}
- B - Median-----(2015 summer training #8(Qualifying))
- C - Colorful Rainbows----(2015 summer training #4 (Qualifying))
- K - Kinds of Fuwas----(2015 summer training #4 (Qualifying))
- H - Hurdles of 110m----(2015 summer training #4 (Qualifying))
- B - And Or---(2015 NEUQ_ACM summer training #1)
- B Black Square----(2015 summer training #5)
- ecjtu-summer training #5 B
- ecjtu-summer training #6 B
- F - Stone-----(2015 summer training #11)
- J - Flyer------(2015 summer training #11)
- Tri_integral Summer Training 8 总结
- C - We Love MOE Girls---(2015 summer training #9)
- A - Faulty Odometer---(2015 summer training #3)
- H - Load Balancing---(2015 NEUQ_ACM summer training #1)
- J - Intelligent IME---(2015 summer training #3)
- Problem A. Automated Telephone Exchange----(2015 summer training #5)
- Problem F. Flat----(2015 summer training #5)
- D - Minimum palindrome----(2015 summer training #9)
- 一个没什么特色的DIV+CSS布局,求指正
- 用批处理修改环境变量
- set/multiset用法详解
- Large Object Support大对象支持
- 安装cygwin
- B - Median-----(2015 summer training #8(Qualifying))
- 《深入理解Android 卷III》第四章 深入理解WindowManagerService
- uva 10099 The Tourist Guide
- 编码转换(utf8转换为gb2312)
- HDU 1215 七夕节
- quartz 动态配置任务
- select、poll、epoll之间的区别总结
- JPA学习笔记(3)——JPA注解
- Linux常用命令大全