当前位置:网站首页>小C的数组(array)
小C的数组(array)
2022-06-30 04:22:00 【MC快乐的苦Xiao怕】
题目描述:
题目描述
小 C 终于成为一名萌新 OIer,最近他在学习数组。
小 C 要练习数组。一次,小 C 得到了一个长度为 n 的数组 a。
现在,对于每一个下标 i,小 C 想找出比 i 小. 且距离 i 最近的下标 j,使得满足 a[i] != a[j],如果不存在,则 j = 0。记下标 i 对应的答案f[i] = j,小 C 为了确保自己的程序正确,想让你来检查 f 数组。
可你不能告诉他整个答案,你只需要告诉他 f 数组所有元素的和即可。
输入格式
从文件 array.in 中读取数据。
共两行,第一行一个正整数 n,表示数组长度;
第二行 n 个正整数,第 i 个表示 ai。
输出格式
输出到文件 array.out 中。
仅一行一个数,表示 f 数组所有元素的和。
输入输出样列
输入样例1:
6
1 1 2 3 2 1
输出样例1:
14
输入样例2:
12
3 3 3 3 2 2 2 2 4 4 1 1
输出样例2:
52
说明
【样例 1 解释】
f[i]依次为 (0, 0, 2, 3, 4, 5),总和为 14。
【样例 2 解释】
f[i]依次为 (0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 10, 10),总和为 52。
【数据范围】
对于 40% 的数据:n ≤ 1000,1 ≤ a[i] ≤ 10,且保证数据随机;
对于 70% 的数据:n ≤ 10^6,1 ≤ a[i] ≤ 10,且保证数据随机;
对于 90% 的数据:n ≤ 10^6,1 ≤ a[i] ≤ 10;
对于 100% 的数据:n ≤ 10^6,1 ≤ a[i] ≤ 1000。
随机数据的生成方式如下:对于每一个 a[i],等概率地从 1 到 10 中产生。
思路1:暴力枚举,对于a[i]从a[1]枚举到a[i - 1]进行检查。
思路2:
像像a[i]与a[i - 1]的关系:如果a[i] != a[i - 1]那么f[i]就是i - 1,否则f[i]就是f[i - 1]
代码:
#include <bits/stdc++.h>
using namespace std ;
const int N = 1000010 ;
int n , res ;
int x[N] , f[N] ;
int main ()
{
cin >> n ;
for (int i = 1; i <= n; i++)
{
cin >> x[i] ;
if (x[i] != x[i - 1]) f[i] = i - 1 ;
else f[i] = f[i - 1] ;
}
for (int i = 1; i <= n; i++) res = res + f[i] ;
cout << res << endl ;
return 0 ;
}
边栏推荐
- Matlab reads fig file and restores signal
- Threejs实现模拟河流,水面水流,水管水流,海面
- Myrpc version 0
- How the FortiGate firewall rejects a port by using the local in policy policy
- Explain the underlying principles of JVM garbage collection in simple terms
- Daily summary of code knowledge
- FortiGate firewall filters the specified session and cleans it up
- Interview topic of MySQL
- Project safety and quality
- Sql语句遇到的错误,求解
猜你喜欢
![[Thesis reading | deep reading] dane:deep attributed network embedding](/img/c7/60f36c2748b8cd7544b7ef14dc309e.png)
[Thesis reading | deep reading] dane:deep attributed network embedding

Threejs实现模拟河流,水面水流,水管水流,海面

Day 11 script and game AI

Es2017 key summary

I get n offers in two months. I don't have any difficult interviewers here

Myrpc version 3

The school training needs to make a registration page. It needs to open the database and save the contents entered on the registration page into the database

FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface

lego_ Reading and summary of loam code

A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
随机推荐
Quick sort & merge sort
Redis cache avalanche, breakdown and penetration
Error encountered in SQL statement, solve
基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
OneNote production schedule
Myrpc version 2
Graduation project EMS office management system (b/s structure) +j2ee+sqlserver8.0
Myrpc version 6
el-upload上传文件(手动上传,自动上传,上传进度)
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code
进程间通信之匿名管道
【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion
深度融合云平台,对象存储界的“学霸”ObjectScale来了
Share an example of a simple MapReduce method using a virtual machine
Cloud native -- websocket of Web real-time communication technology
JS file block to Base64 text
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
Threejs实现模拟河流,水面水流,水管水流,海面
Maya Calendar(POJ1008)
网络层详解