#421 Div.2 D. Mister B and PR Shifts
来源:互联网 发布:主机所在域的域名 编辑:程序博客网 时间:2024/06/03 18:27
题目链接:
http://codeforces.com/contest/820/problem/D
大意:
给一个数组 p[i]
求 值。
每次操作旋转一次数组(即最后一个数字变为第一个) 如 1 2 3 -> 3 1 2
求 的最小值 输出最小值和变换次数。
input
3
1 2 3
output
0 0
input
3
2 3 1
output
0 1
input
3
3 2 1
output
2 1
#include<bits/stdc++.h>using namespace std;#define D(v) cout<<#v<<" "<<v<<endl#define LL long longint p[1000005], cur[2000005];int main(void){ LL ans,sum; int n,L,r,i,temp; scanf("%d", &n); sum = L = r = temp = 0; for(i=1;i<=n;i++) scanf("%d", &p[i]); for(i=1;i<=n;i++) { sum += abs(p[i]-i); if(p[i]>=i) L++, cur[p[i]-i]++;//cur[i]可以理解为再向后移动i位后a[i]-i>=0。 else r++; } ans = sum; for(i=0;i<n-1;i++) { L -= cur[i]; r += cur[i]; sum = sum-L+r-abs(p[n-i]-n-1)+abs(p[n-i]-1);//相当于p[n-i]先移到后面的n+1位置 再从n+1移到1 cur[p[n-i]+i]++; //重新更新 L , r 和 cur[] L++, r--; //... if(sum<ans) ans=sum,temp=i+1; } printf("%lld %d\n",ans,temp); return 0;}
参考博客:
http://www.cnblogs.com/TnT2333333/p/7091392.html
阅读全文
0 0
- #421 Div.2 D. Mister B and PR Shifts
- Codeforces Round 421 Div.2 D. Mister B and PR Shifts 思维 乱搞
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
- CodeForces-820D Mister B and PR Shifts
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts(瞎搞)
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts 思维,乱搞
- CF 820D Mister B and PR Shifts 差分(区间+k*(x-L)+b)
- CodeForces 820D Mister B and PR Shifts(思维题)
- Codeforces 819 B. Mister B and PR Shifts
- Codeforces 819B. Mister B and PR Shifts
- codeforces819B Mister B and PR Shifts -- 线段树
- Codeforces Round #421 Mister B and PR Shifts(暴力瞎搞)
- codeforces819bMister B and PR Shifts
- Codeforces Round #421 (Div. 2) B. Mister B and Angle in Polygon
- Codeforces Round #421 (Div. 2) | B. Mister B and Angle in Polygon
- #421 Div.2 B. Mister B and Angle in Polygon——几何数学
- Codeforces Round #421 (Div. 2) A. Mister B and Book Reading
- 死锁
- 对文件的操作
- spring could 微服务 跨域问题(CORS )
- Use coccinelle to check patch in Ubuntu 14.04
- linux下TCP/IP及内核参数优化调优
- #421 Div.2 D. Mister B and PR Shifts
- 同步异步,阻塞非阻塞的区别
- java.sql.SQLException: Access denied FOR USER 'test'@'DESKTOP-KQLR3L8' (USING PASSWORD: YES)
- tf随笔-1
- Java基础教程41-读取文件内容
- 20170703学习笔记
- 微信小程序开发之选择器 时间选择器 日期选择器 地区选择器
- 程序员Windows上应该知道的软件开发神器
- IP地址的分类