当前位置:网站首页>A.前缀极差(C语言)
A.前缀极差(C语言)
2022-06-11 03:38:00 【洛907】
一、题目
蒜头君有 nnn 个数,他提出了 qqq 个问题,每个问题是说,询问前 xxx 个数的极差(最大值减最小值)。你能帮助他解决这 qqq 个问题吗?
Input
第一行两个整数 n,q(1≤n,q≤105)n, q(1 \leq n, q \leq 10 ^ 5)n,q(1≤n,q≤105)
第二行 nnn 个整数 ai(1≤ai≤109)a_i(1 \leq a_i \leq 10 ^ 9)ai(1≤ai≤109)
表示蒜头君的 nnn 个数第三行 qqq 个整数 xi(1≤xi≤n)x_i(1 \leq x_i \leq n)xi(1≤xi≤n) ,表示每一次询问
Output
输出一行,包含 qqq 个整数,表示每一次询问的答案
Sample 1
Inputcopy Outputcopy
5 5
3 2 4 5 1
1 2 3 4 5
0 1 2 3 4
二、解决方案
1.思路
①这道题根据题意,我们要计算先x项的极差,那我们就要知道前x项的最大值与最小值,在之前写的代码中,我在for循环里每一次计算前x项的极差,最后的结果是超时的。
②所以为了更简化我们的代码,我们再输入数字的时候就提前把它前x项的最大值与最小值计算出来,后面直接用即可,就避免了重复的运算
——————————————
2.代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n=0,q=0;
scanf("%d %d",&n,&q);
int arr[100005]={
0};
int max[100005]={
0},min[100005]={
0};
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
if(i==1)
{
max[i]=arr[i],min[i]=arr[i];
}
else
{
max[i] = max[i-1]>arr[i]?max[i-1]:arr[i];
min[i] = min[i-1]<arr[i]?min[i-1]:arr[i];
}
}
int x=0;
for(int i=0;i<q;i++)
{
scanf("%d",&x);
printf("%d ",max[x]-min[x]);
}
return 0;
}
边栏推荐
- Go failing - expected ‘package‘, found ‘EOF‘
- RT thread test
- Synchronized locked objects
- Notes on redisson distributed lock usage
- Samsung Galaxy S21 ultra and Apple iPhone 13 Pro Max: which one should you choose
- 给孩子的国学经典
- VIM quickly select a method / function
- [elt.zip] openharmony paper Club - fast random access string compression
- Using minted to insert highlighted code in texstudio in latex environment
- WPF of open source project hero alliance
猜你喜欢

Detailed explanation of scenario method for common test case design methods

MAUI 遷移指南

OpenGL Chapter 10 illuminant

J. Balanced Tree

Shell script binary encryption
![[signalr complete series] Net6 Zhongshi signalr communication](/img/af/4b8bfea6238c646c54352635d5da98.png)
[signalr complete series] Net6 Zhongshi signalr communication

Quartz: an old and robust open source task scheduling framework, which is smooth enough to use

The key data of music genuine rate is missing. What is the odds of Netease cloud music IPO?

What has TCL done right to break through the technological strength of Chinese brand innovation?

Student teacher examination management system based on SSM framework
随机推荐
Kirin V10 installation of tongweb7.0
Unity's data persistence -- Jason
Host computer development (how to develop host computer)
Record the problem of Galaxy Kirin V10 server version once: an error is reported when installing KVM
Optimize your code execution efficiency with completabilefuture
Maui migration guide
基于SSM的大学生社团管理系统
[cnn]| translation invariance
SSL交互过程
让人感到心灵平静,阳光温暖的图片
Thinkphp3.2.3 deserialization using chain analysis
RHEL7 切换字符编码为GBK
PMM monitoring Oracle
1_ Attribute management function
大厂外包or自研公司?测试人找工作怎么选?
rt-thread测试
OpenGL error Guide
Guide de migration Maui
C. Jump and Treasure(dp + 单调队列优化)
Interface performance optimization ideas