Codeforce#424E. Cards Sorting(脑洞+树状数组)
来源:互联网 发布:centos编译安装php5.6 编辑:程序博客网 时间:2024/06/06 05:20
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.
Print the total number of times Vasily takes the top card from the deck.
46 3 1 2
7
11000
1
73 3 3 3 3 3 3
7
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int sum[maxn], n;vector<int> v[maxn];void modify(int x, int val){ while(x <= n) { sum[x] += val; x += x & (-x); }}int pre = 1;bool cmp(int u, int v){ int u1 = (u - pre + n) % n; int v1 = (v - pre + n) % n; return u1 < v1;}int getsum(int x){ int ret = 0; while(x > 0) { ret += sum[x]; x -= x & (-x); } return ret;}int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) { int num; scanf("%d", &num); v[num].push_back(i); modify(i, 1); } for(int i = 1; i <= 100000; i++) if(v[i].size()) { sort(v[i].begin(), v[i].end(), cmp); pre = v[i][v[i].size()-1]; } pre = 0; ll ans = 0; for(int i = 1; i <= 100000; i++) { if(!v[i].size()) continue; for(int j = 0; j < v[i].size(); j++) { int now = v[i][j]; if(now > pre) ans += getsum(now) - getsum(pre); else ans += getsum(n) - (getsum(pre) - getsum(now)); pre = now; modify(now, -1); } } printf("%I64d\n", ans); return 0;}
题意:有一堆牌,不断的拿第一张,如果这张牌是此时最小的牌,则拿掉,如果不是,则放到牌底
树状数组,每个点刚开始为1,拿走的置为0,无论怎么换,其实顺序是不变的,特殊修改一下即可,对于数值相同的牌,要有一个拿的顺序,由前面拿完(数值比他小且紧挨着的)后的位置决定,可以在排序的时候修改一下cmp。
- Codeforce#424E. Cards Sorting(脑洞+树状数组)
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- Codeforces Round #424 (Div. 2) E. Cards Sorting(思维 树状数组)
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- Codeforces 387E George and Cards(二分+树状数组)
- George and Cards - CodeForces 387E 树状数组
- Codeforces 387E George and Cards(二分+树状数组)
- codeforces 831 E Cards Sorting(bit+stl)
- Codeforces #831E: Cards Sorting 题解
- Codeforces 831-E.Cards Sorting(线段树)
- Cow Sorting (树状数组)
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- 增删改查测试点
- 文件读取、写入
- Swift 空格
- 相机中的两个焦距FX,FY
- Unity 碰撞器 简介
- Codeforce#424E. Cards Sorting(脑洞+树状数组)
- 海思Hi3516A开发板RTSP H.265/H.264编码HDMI输入1080P60帧高清
- thinkphp在volist中 如何url传值,
- MYSQL数据库命名及设计规范
- Tensorflow深度学习之四:placeholder和Session
- FreeBSD开发手册中文版
- spark 基础知识- spark SQL专题
- 文章解析整理:《十分钟看懂图像语义分割技术》
- 基于Swift的iOS应用程序开发:使用表格显示并控制数据(三):批量处理表格数据