codeforces 831 E Cards Sorting(bit+stl)
来源:互联网 发布:java工作流activity 编辑:程序博客网 时间:2024/06/05 05:22
题意:
n个牌,牌上有数,每次从牌顶取出一张牌,如果这张牌是当前的最小值那么就把它扔掉,不然放到牌堆底,问需要多少次操作把牌扔光。
解题思路:
将牌记录下初始位置,按大小排序,然后按顺序取出,用树状数组查询扔牌的时候前面访问了多少张牌。
但是需要注意个问题,从一个最小值取完的时候,下一个最小值的牌的选择应该要从上一次扔牌的右边第一张开始。
比如
3
2 1 2
这组数据,扔出1后,我们应该选取第3个位置的2,而不是第一个位置的2。
所以可以把值相同的牌存在一个set里,每次一个set取完时,对下一个set进行二分查找上一次扔牌的右边第一个坐标。
每次扔出的牌不再从右边选取之后,进行一次查询,查询的坐标不是当前坐标,而应该就是n
代码:
#include <bits/stdc++.h>#define LL long longusing namespace std;const int maxn=1e5+5;struct node{ LL x; int id;}a[maxn];bool cmp(node a, node b){ return a.x<b.x;}long long val[maxn];int lowbit(int x){ return x&-x;}void add(int x, int y){ while(x<maxn) { val[x]+=y; x+=lowbit(x); } return;}long long sum(int x){ long long res=0; while(x>0) { res+=val[x]; x-=lowbit(x); } return res;}set<int>s[maxn];set<int>::iterator it;int main(){ int n; cin>>n; int i; LL j; for(i=1; i<=n; i++) { scanf("%lld", &a[i].x); a[i].id=i; } sort(a+1, a+1+n, cmp); j=0; s[j].insert(a[1].id); for(i=2; i<=n; i++) { if(a[i].x==a[i-1].x)s[j].insert(a[i].id); else { j++; s[j].insert(a[i].id); } } int m=j; int last, id; j=0; for(i=1; i<=n; i++) { id=a[i].id; add(id, 1); } LL ans=0; last=0, j=0; for(i=0; i<=m; i++) { auto mid=s[i].upper_bound(last); for(it=mid; it!=s[i].end(); it++) { // printf("%d\n", *it); j++; add(*it, -1); last=*it; } for(it=s[i].begin(); it!=mid; it++) { // printf("%d\n", *it); if(it==s[i].begin()) { ans+=j+sum(n); // printf("%lld %d %lld\n", j, last, ans); j=0; } add(*it, -1); j++; last=*it; } }// cout<<ans<<endl; cout<<ans+j<<endl; return 0;}
阅读全文
0 0
- codeforces 831 E Cards Sorting(bit+stl)
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces #831E: Cards Sorting 题解
- Codeforces 831-E.Cards Sorting(线段树)
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- codeforces 830B. Cards Sorting
- Codeforces B. Cards Sorting 【瞎搞】
- Codeforces Round #424 (Div. 2) E. Cards Sorting(思维 树状数组)
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E Cards Sorting
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- Codeforce#424E. Cards Sorting(脑洞+树状数组)
- CodeForces 387E George and Cards
- [DP]Codeforces 743E Vladik and cards
- 不使用加减乘除计算数字相加问题
- Android引导页
- Tomcat的conf文件夹中的web.xml文件作用
- 使用leakcanary检测Android项目内存泄露
- Delphi XE6 原生解析json
- codeforces 831 E Cards Sorting(bit+stl)
- 2017-07-14
- 代码规范中的PMD规范的通俗理解。
- SpringCloud系列(4)---Feign 声明式REST调用
- MongoDB聚合操作Aggregation
- 市面上的通用版CRM无法实现公司需求该如何破
- 使用友盟完成第三方QQ登录,并回传头像等数据
- 使用可变参数的方法记录log
- LAMP搭建——PHP安装