CF 433C Ryouko's Memory Note
来源:互联网 发布:python内置函数手册 编辑:程序博客网 时间:2024/05/29 14:51
CF 433C Ryouko's Memory Note
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.
Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.
Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from pagex to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is .
Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place.
Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.
The first line of input contains two integers n and m (1 ≤ n, m ≤ 105).
The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n).
Print a single integer — the minimum number of pages Ryouko needs to turn.
4 61 2 3 4 3 2
3
10 59 4 3 8 8
6
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.
In the second sample, optimal solution is achieved by merging page 9 to 4.
执行一次操作 将相邻的纸上的信息完全复制到另一张纸上。。来减少翻得书的页数
用vector来存储 相邻的数 排除相邻的数相等的情况 记录下当前所需翻的页数
将相邻的页数排序 取出中位数 替代当前的数 与之前所需要翻得页数进行比较 取最小值
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#define eps 1e-8#define op operator#define MOD 10009#define MAXN 100100#define FOR(i,a,b) for(int i=a;i<=b;i++)#define FOV(i,a,b) for(int i=a;i>=b;i--)#define REP(i,a,b) for(int i=a;i<b;i++)#define REV(i,a,b) for(int i=a-1;i>=b;i--)#define MEM(a,x) memset(a,x,sizeof a)#define ll __int64using namespace std;int a[MAXN];vector<int> v[MAXN];int main(){//freopen("ceshi.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int mx=0; for(int i=0;i<m;i++) { scanf("%d",&a[i]); mx=max(mx,a[i]); } ll sum=0; for(int i=1;i<m;i++) { if(a[i]==a[i-1]) continue; v[a[i]].push_back(a[i-1]); v[a[i-1]].push_back(a[i]); sum+=(abs(a[i]-a[i-1])); } ll mi=sum;// cout<<sum<<endl; vector<int> x; for(int i=1;i<=mx;i++) { ll temp=sum; if(v[i].size()==0) continue; x=v[i]; sort(x.begin(),x.end()); int y=x[x.size()/2];//被替换之后的数 for(int j=0;j<x.size();j++) { temp+=(abs(x[j]-y)-abs(i-x[j])); } mi=min(mi,temp); } printf("%I64d\n",mi); } return 0;}
- CF 433C Ryouko's Memory Note
- CF 248 C. Ryouko's Memory Note
- Codeforces 433C Ryouko's Memory Note
- Codeforces 433 C. Ryouko's Memory Note
- Codeforces 433 C Ryouko's Memory Note
- Codeforces 433C Ryouko's Memory Note
- codeforces #433C Ryouko's Memory Note
- C. Ryouko's Memory Note
- CodeForces 433C Ryouko's Memory Note(暴力加中位数)
- codeforces #433C Ryouko's Memory Note(瞎搞)
- CodeForces 433C Ryouko's Memory Note (数学)
- Codeforces433 C Ryouko’s Memory Note
- Codeforces Round #248 (Div. 2) C.Ryouko's Memory Note
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
- Codeforces Round #248 (Div. 2) C - Ryouko's Memory Note
- Ryouko's Memory Note(书本翻页)
- codeforces248(div1) A. Ryouko's Memory Note
- Codeforces RoundC. Ryouko's Memory Note
- 黑马程序员——io流2(其它流)
- 在ubuntu14.04下配置myeclipse 2014及svn插件
- 黑马程序员17——OC之Fundation(NSDictionary)
- [Android分享] Android 分辨率自适应
- 内核态与用户态的区别
- CF 433C Ryouko's Memory Note
- unix/linux编程实践教程:数据报编程
- poj1061青蛙的约会
- AudioManager使用方法
- gcc的使用
- 异常处理系列:Android开发问题:Failed to install Hello.apk on device 'emulator-5554!
- java基础复习(7)
- IOS代码打电话
- 浅谈硬盘引导记录