Educational Codeforces Round 4 B. HDD is Outdated Technology 暴力
#B. HDD is Outdated Technology
##题目连接:
http://www.codeforces.com/contest/612/problem/B
##Description
HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.
One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.
Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.
It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.
##Input
The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments.
The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.
##Output
Print the only integer — the number of time units needed to read the file.
##Sample Input
3
3 1 2
##Sample Output
3
##Hint
##题意
给你n个数,一开始你在1,然后要走到2,然后走到3
问你最后他走到n的时候,花费多少
题解:
直接存一下每个值所在的位置,然后扫一遍就好了
代码
#include<bits/stdc++.h>
using namespace std;
int b[300000];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
b[x]=i;
}
long long ans = 0;
for(int i=1;i<n;i++)
ans+=abs(b[i+1]-b[i]);
cout<<ans<<endl;
}
Educational Codeforces Round 4 B. HDD is Outdated Technology 暴力的更多相关文章
- Educational Codeforces Round 4 B. HDD is Outdated Technology
题目链接:http://codeforces.com/problemset/problem/612/B 解题思路: 一开始看错了题意,他要求的是从1-n所耗费的时间,n表示的是数值而不是下标, 实现代 ...
- Educational Codeforces Round 1 B. Queries on a String 暴力
B. Queries on a String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/59 ...
- Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 22 B. The Golden Age(暴力)
题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...
- Educational Codeforces Round 15 A, B , C 暴力 , map , 二分
A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 6 B. Grandfather Dovlet’s calculator 暴力
B. Grandfather Dovlet’s calculator Once Max found an electronic calculator from his grandfather Do ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- memcache、memcached、groupcache的区别
对PHP语言来说,PHP使用memcache有两个模块,分别叫memcache和memcached,他们的区别看下表: 参考:http://hi.baidu.com/tony_wd/item/605e ...
- HDU3333 Turing Tree 离线树状数组
题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...
- 【LeetCode 234】Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后 ...
- Cocos2d-android (03) 向量
向量的基本运算及动作 import org.cocos2d.actions.interval.CCJumpBy; import org.cocos2d.actions.interval.CCMoveB ...
- Cracking the Code Interview 4.3 Array to Binary Tree
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- TLCL中英对照版
TLCL中英文对照阅读网址:http://billie66.github.io/TLCL/book/index.html 感谢好奇猫团队(http://haoqicat.com/about/team) ...
- 程序语言的奥妙:算法解读 ——读书笔记
算法(Algorithm) 是利用计算机解决问题的处理步骤. 算法是古老的智慧.如<孙子兵法>,是打胜仗的算法. 算法是古老智慧的结晶,是程序的范本. 学习算法才能编写出高质量的程序. 懂 ...
- Dreamweaver SSH Tunneling
- html5 base64基础
base64常见的编码形式,二进制文件.图片.视频等 如何弄出来一个base64? a). FileReader readAsDataURL(); b). 工具 ...