当前位置:网站首页>小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 ;
}
边栏推荐
- Idea grey screen problem
- Share an example of a simple MapReduce method using a virtual machine
- Error in conditional filter (if) syntax in sum function in SQL Server2005
- Huawei cloud native - data development and datafactory
- Interface testing -- how to analyze an interface?
- Educoder group purchase suspension box page production
- 技术分享| 融合调度中的广播功能设计
- SQL append field
- Myrpc version 5
- [learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
猜你喜欢

《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述

About manipulator on Intelligent Vision Group

Myrpc version 4

Cloud native -- websocket of Web real-time communication technology

How to solve the problem of link hyperlinks when trying to link the database?

Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)

Day 9 script and resource management

深度融合云平台,对象存储界的“学霸”ObjectScale来了

DBT product initial experience

Day 12 advanced programming techniques
随机推荐
Thinkphp5 implements import function
Grasp grpc communication framework in simple terms
Graduation project EMS office management system (b/s structure) +j2ee+sqlserver8.0
RPC correction based on arcpy API
7-3 打怪升级 单源最短路
Clients accessing the daytime service (TCP)
技术分享| 融合调度中的广播功能设计
第九天 脚本與資源管理
Ananagrams(UVA156)
How to solve the problem of link hyperlinks when trying to link the database?
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
JS generator
网络层详解
JS proxy
找到接口在表单里加参数
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
Error in conditional filter (if) syntax in sum function in SQL Server2005
[learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
JS deconstruction assignment
Machine learning notes