Codeforces Round #424 E. Cards Sorting
题目大意:给你一堆n张牌(数字可以相同),你只能从上面取牌,如果是当前牌堆里面最小的值则拿走,
否则放到底部,问你一共要操作多少次。
思路:讲不清楚,具体看代码。。
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const int N=1e5+;
ll n,mxi[N],a[N];//a[i]保存原始数据,mxi[i]保存大小为i的牌最下面一张的编号
vector<ll> p[N];//p[i] 保存值为i的所有牌的编号,从小到大。
ll solve()
{
sort(a+,a+n+);//先从小到大排序
ll now_n=n;//now_n表示到目前为止还剩多少张牌
ll ans=now_n;//第一次操作取全部牌,扔掉大小为a[1]的。
now_n-=p[a[]].size();
p[a[]].clear();
for(int i=;i<=n;i++)//从a[2]开始
{
if(p[a[i]].empty()) continue;//如果这个大小的都取完了跳过。
if(p[a[i]].back()>mxi[a[i-]])//如果当前a[i]的最底下一个的编号大于
{ //a[i-1]最底下的编号,则在mxi[a[i-1]]下面的编号
//在上一次取a[i-1]的时候就取掉了。
vector<ll> ::iterator it;
it=lower_bound(p[a[i]].begin(),p[a[i]].end(),mxi[a[i-]]);
ll num=p[a[i]].end()-it;
p[a[i]].erase(it,p[a[i]].end());
if(!p[a[i]].empty()) mxi[a[i]]=p[a[i]].back();
now_n-=num;//减掉num为当前的牌数。
}
else//取出全部牌将a[i]大小的牌扔掉。
{
ans+=now_n;
now_n-=p[a[i]].size();
p[a[i]].clear();
}
}
return ans;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
p[a[i]].pb(i);
mxi[a[i]]=i;
}
ll ans=solve();
cout<<ans<<endl;
return ;
}
Codeforces Round #424 E. Cards Sorting的更多相关文章
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组
E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)
Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #424 Div2 E. Cards Sorting
我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...
- 【Splay】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) B. Cards Sorting
Splay要支持找最左侧的最小值所在的位置.类似线段树一样处理一下,如果左子树最小值等于全局最小值,就查左子树:否则如果当前节点等于全局最小值,就查当前节点:否则查右子树. 为了统计答案,当然还得维护 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C
A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...
随机推荐
- PB9.0数据窗口入门
因为工作的关系,最近公司用PB比较多,至少10年没用过PB了,回顾一下数据窗口使用方法.1.代码结构 2.数据窗口 3.代码窗口代码 APP入口代码 4.需要使用DataBase插入一条数据 5.执行 ...
- MyBatis向数据库中批量插入数据
Foreach标签 foreach: collection:指定要遍历的集合; 表示传入过来的参数的数据类型.该参数为必选.要做 foreach 的对象,作为入参时,List 对象默认用 list 代 ...
- C++ 中 #ifndef, #define, #endif 宏定义
目的:为了保证包含的内容只被程序(include) 和编译了一次.判断预处理器常量是否已被定义. 预编译将所有头文件(#include"XXX.h")用头文件中的内容来替换,头文件 ...
- win7防火墙端口开放
https://jingyan.baidu.com/article/f96699bbadafca894f3c1b7a.html
- 矩阵的SVD分解
转自 http://blog.csdn.net/zhongkejingwang/article/details/43053513(实在受不了CSDN的广告) 在网上看到有很多文章介绍SVD的,讲的也都 ...
- SpringMvc整合Mybatis并使用声明式事务
(1).引入相关依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid< ...
- 【网络编程3】网络编程基础-arp请求(局域网主机扫描)
ARP协议 ARP(Add ress Resolution Protocol)地址解析协议位于数据链路层,是根据IP地址获取MAC地址的一个协议. ARP 查看指令 arp -a 显示所有接口的当前A ...
- MySQL— 进阶
目录 一.视图 二.触发器 三.函数 四.存储过程 五.事务 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集 ...
- 【Python学习笔记】调整matplotlib的图例legend的位置
有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...
- IAR KEIL ECLIPSE使用JlinkScript文件进行调试
转载自:https://wiki.segger.com/Using_J-Link_Script_Files Using J-Link Script Files Contents [hide] ...