Andryusha and Socks
来源:互联网 发布:linux命令view 编辑:程序博客网 时间:2024/05/27 08:13
题目链接 http://codeforces.com/problemset/problem/780/A
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
Print single integer — the maximum number of socks that were on the table at the same time.
11 1
1
32 1 1 3 2 3
2
一个人有n双袜子,放在包里面(但是这些袜子都是混在一起的,也就是说不配套的),他想把这些袜子整理在衣柜里面(同一双的放在一起)。所以他就从包里面一次拿一只,如果这只袜子和之前拿出来的是一套,就放在衣柜里面;如果和以前拿出来的都不配套,就放在桌子上面,就这样一次次的拿,直到拿完这n双袜子。题目问桌子上最多放几双袜子?并输出。
解题思路:
这道题就是简单的map的应用,不会做的应该是看不懂题意。题意懂了,代码就容易了。
代码:
#include<iostream>#include<map>using namespace std;int main(){ int n,sum,max,s;//sum保存目前桌子上的袜子数目,max保存桌子上最大的袜子数目 while(cin>>n) { sum=0; max=0; map<int,int>M; //定义一个map容器 for(int i=1;i<=2*n;i++) { cin>>s; M[s]++; if(M[s]==1)//如果目前这只袜子以前没有出现过,那么桌子上的咋子数目sum++ { sum++; if(sum>max)//max始终保存最大数量的袜子的数目 max=sum; } else sum--; } cout<<max<<endl; } return 0;}
- A. Andryusha and Socks
- Andryusha and Socks
- Codeforces 782 Andryusha and Socks
- 【codeforces 782A】Andryusha and Socks
- cf 782A Andryusha and Socks
- Codeforces 780A-Andryusha and Socks
- cf 782A Andryusha and Socks
- CodeForces 780A Andryusha and Socks【水题】
- codeforces 782a Andryusha and Socks
- 【codeforce】403 Div.2 A Andryusha and Socks
- CF-Round#403 Div2 A. Andryusha and Socks
- Codeforces Round #403 (Div. 2) A. Andryusha and Socks(简单模拟)
- Andryusha and Socks_Codeforces
- Andryusha and Colored Balloons
- codeforces781A Andryusha and Colored Balloons
- Codeforces Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- 借口——是你最大的敌人
- sql入门经典总结
- Android导入eclipse项目出现Error: A library uses the same package as this project
- 学生总结(一)
- 【转载】大学生必备软件免费自学视频教程,让那些培训班都见鬼去吧
- Andryusha and Socks
- 【转载】教你建一个别人打不开的文件夹
- 扩展中国剩余定理模板
- WEB-INF安全目录下资源不能直接访问的问题
- iOS 分类和扩展
- 一起学ORBSLAM2(3)system框架搭建
- 局域网聊天室
- 217. Contains Duplicate
- 花园