CodeForces612BHDD is Outdated Technology(sort,结构体,水题)
2016-07-21 20:23
405 查看
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
Input
3
3 1 2
Output
3
Input
5
1 3 5 4 2
Output
10
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct st{
__int64 nm;
__int64 q;
}a[200001];
bool cmp(st x,st y)
{
return x.nm<y.nm;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF)
{
for(__int64 i=0;i<n;i++)
{
scanf("%I64d",&a[i].nm);
a[i].q=i;
}
sort(a,a+n,cmp);
__int64 sum=0;
for(__int64 i=0;i<n;i++)
{
if(i!=n-1)
sum+=abs(a[i+1].q-a[i].q);
}
printf("%I64d\n",sum);
}
return 0;
}思路:
给你一个打乱顺序的数组n,让你从数值为1的数一直按顺序走到n,问总共走多少步。
直接将数保存在结构体数组内,并在结构体内另保存一个数记录初始地址。sort快排后利用保存的初始地址算步数,然后输出就可以了。
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
Input
3
3 1 2
Output
3
Input
5
1 3 5 4 2
Output
10
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct st{
__int64 nm;
__int64 q;
}a[200001];
bool cmp(st x,st y)
{
return x.nm<y.nm;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF)
{
for(__int64 i=0;i<n;i++)
{
scanf("%I64d",&a[i].nm);
a[i].q=i;
}
sort(a,a+n,cmp);
__int64 sum=0;
for(__int64 i=0;i<n;i++)
{
if(i!=n-1)
sum+=abs(a[i+1].q-a[i].q);
}
printf("%I64d\n",sum);
}
return 0;
}思路:
给你一个打乱顺序的数组n,让你从数值为1的数一直按顺序走到n,问总共走多少步。
直接将数保存在结构体数组内,并在结构体内另保存一个数记录初始地址。sort快排后利用保存的初始地址算步数,然后输出就可以了。
相关文章推荐
- php 里邮箱找回密码 的正确格式
- Hibernate 官方文档(入门) 第一章 1.3 - The EventManager web application 简单的web应用
- Android如何通过shareduserid获取系统权限
- 字符串中查找字母有几个
- 个人学习博客1
- Uncowed Forces
- DOM—动态添加table
- 两个大的整数的运算(java)
- Hotel
- Notification
- NYOJ 330 一个简单的数学题【思维】
- HDU 1241 Oil Deposits 油田 搜索
- Restaurant
- Hibernate中,One2Many和Many2One的双向映射关系的XML实现方式
- 用slf4j+logback实现多功能日志解决方案 --- 转
- linker command failed with exit code 1 (use -v to see invocation)
- linker command failed with exit code 1 (use -v to see invocation)
- HDU 4268-Alice and Bob-贪心
- ifconfig command not found
- 告诉你一个java对象占用的内存大小的方法