codeforces 830B. Cards Sorting
2017-07-15 16:31
288 查看
题目链接
B. Cards Sorting分析
这是一道很脑洞的玄学题.我们可以发现提取数字的顺序一定是从小到大的,每个数字需要用1次+转移到后面的次数。那问题的关键就是计算转移到后面的次数。我们可以发现每个数字转移到后面的次数等于在它后面比他小的次数+1,因此我们可以记录每个数字(设当前数字为 i )出现的位置,从小到大排列,记录最后出现的位置为 p ,用的次数为 (dis)那么 i 后面的数字 i+1(假设存在) 就被p分为两段(二分),在 p 之后的,和 p 之前的,在 p 之后的数只需要 dis次,在p之前的需要 dis+1次,依次从小到大扫描每个数得到正确答案python3 AC code
from bisect import bisect_left def read(): return [int(x) for x in input().split()] n = read() a = read() vec = [list() for i in range(max(a)+1)] for i in range(len(a)): vec[a[i]].append(i) def solve(): ans =0 dis = 1 p = 0 i=0 while 4000 i <len(vec): if len(vec[i]): pos = bisect_left(vec[i],p) ans += (len(vec[i]) - pos)*dis p = vec[i][len(vec[i])-1] # print(i,pos,dis,ans,p) # print(vec[i]) del vec[i][pos:len(vec[i])] if len(vec[i]): p = 0 dis+=1 i-=1 i+=1 return ans print(solve())
c++ AC code
#include<bits/stdc++.h> #define pb push_back #define mp make_pair #define PI acos(-1) #define fi first #define se second #define INF 0x3f3f3f3f #define INF64 0x3f3f3f3f3f3f3f3f #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define ms(x,v) memset((x),(v),sizeof(x)) using namespace std; const int MOD = 1e9+7; const double eps = 1e-8; typedef long long LL; typedef long double DB; typedef pair<int,int> PII; const int maxn= 1e5+10; vector<int> vec[maxn]; int a[maxn]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n;cin>>n; for(int i=0 ; i<n ; ++i){cin>>a[i];vec[a[i]].pb(i);} LL ans =0,dis = 1,p=0; for(int i=1; i<maxn ; ++i){ if(vec[i].empty())continue; vector<int> :: iterator iter = lower_bound(vec[i].begin(),vec[i].end(),p); ans += dis*(vec[i].end()-iter); // cout<<i<<" "<<(vec[i].end()-iter)<<" "<<p<<"\n"; // for(auto e : vec[i]){cout<<e<<" ";} // cout<<"\n"; p = vec[i].back(); vec[i].erase(iter,vec[i].end()); if(vec[i].size()){ p=0;dis++;//在元素i-1前的元素增加一次时间从新计算 i--; } } cout<< ans <<"\n"; return 0; }
相关文章推荐
- Codeforces 828 C String Reconstruction
- Codeforces 893E 数论
- CodeForces 225C Barcode
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
- codeforces 339d(线段树)
- CodeForces 831C Jury Marks
- CodeForces - 616D Longest k-Good Segment 尺取
- Codeforces-705B-Spider Man
- CodeForces 144A Arrival of the General
- codeforces 557D Vitaly and Cyclef(二分图染色)
- CodeForces 831A-Unimodal Array
- CodeForces - 848A From Y to Y
- codeforces 703D D. Mishka and Interesting sum(树状数组)
- CodeForces 1A Theatre Square
- CodeForces 197B Limit
- CodeForces 830B - Cards Sorting
- CodeForces - 551C (二分查找答案+贪心)
- CodeForces-622A.Infinite Sequence
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
- Codeforces 558C Amr and Chemistry 全都变相等