区间sum 和为k的连续区间-前缀和
区间sum
有一个长度为n的正整数序列a1--an,candy想知道任意区间[L,R]的和,你能告诉他吗?
第一行一个正整数n(0<n<=1e6),
第二行为长度为n的正整数序列 a1到an(ai<=1e9,sum(a)<=1e18),
第三行一个正整数m(m<=1e6),表示询问次数,
随后m行,每行两个正整数L,R(1<=L<=R<=n)。
m行输出,对应m次询问结果
5
1 4 3 6 3
3
1 3
2 2
1 5
8
4
17
一整数数列a1, a2, ... , an(有正有负),以及另一个整数k,求一个区间i,ji,j,(1 <= i <= j <= n),使得aii + ... + ajj = k。
Input第1行:2个数N,K。N为数列的长度。K为需要求的和。(2 <= N <= 10000,-10^9 <= K <= 10^9)
第2 - N + 1行:Aii(-10^9 <= Aii <= 10^9)。Output如果没有这样的序列输出No Solution。
输出2个数i, j,分别是区间的起始和结束位置。如果存在多个,输出i最小的。如果i相等,输出j最小的。Sample Input
6 10
1
2
3
4
5
6
Sample Output
1 4 前缀和,通常用来求某个区间的和或和为多少的区间,前者时间复杂度由暴力O(n)节省到了O(1),是一个高效的区间和方法。
#include<stdio.h> int main()
{
int n,m,l,r,i;
long long a[];
scanf("%d",&n);
a[]=;
scanf("%lld",&a[]);
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
a[i]+=a[i-];
}
scanf("%d",&m);
for(i=;i<=m;i++){
scanf("%d%d",&l,&r);
printf("%lld\n",a[r]-a[l-]);
}
return ;
}
#include<stdio.h>
#include<string.h>
int main()
{
long long n,k,f,i,j; long long a[],b[];
scanf("%lld%lld",&n,&k);
memset(b,,sizeof(b));
scanf("%lld",&a[]);
b[]=a[];
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
b[i]+=b[i-]+a[i];
}
f=;
for(i=;i<=n;i++){
for(j=i;j<=n;j++){
if(b[j]-b[i-]==k){
printf("%lld %lld\n",i,j);
f=;
break;
}
}
if(f==) break;
}
if(f==) printf("No Solution\n");
return ;
}
区间sum 和为k的连续区间-前缀和的更多相关文章
- 51nod 1094 和为k的连续区间【前缀和/区间差/map】
1094 和为k的连续区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一整数数列a1, a2, ... , an(有正有负),以及另一个整数k ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。
/** 题目:hdu6003 Problem Buyer 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6003 题意:给定n个区间,以及m个数,求从n个区 ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- 如何求文件File的字节数
[java]代码库 import java.io.*; /** * 获取文件的字节数 */ class FileInputStreamS { public static voi ...
- input输入框输入大小写字母自动转换
input输入框输入小写字母自动转换成大写字母有两种方法 1.用js onkeyup事件,即时把字母转换为大写字母: html里input加上 <input type="text&qu ...
- registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
本文转载至 http://bbs.csdn.net/topics/390889517 IOS8 PUSH解决方法 昨天晚上整理PUSH的东西,准备些一个教程,全部弄好之后,发现没有达到预期的效果,本以 ...
- [Phoenix] 二、数据类型
目前Phoenix支持24种简单数据类型和1个一维Array的复杂类型.以下是对支持数据类型的说明: 1. INTEGER 2. UNSIGNED_INT 3. BIGINT 4. UNSIGNED_ ...
- mybatis入门(四)
mybatis入门 需求:根据id查询用户的信息 mysql数据库: CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `us ...
- 数据库的update、delete、insert和select用法
String sql=null; 1.sql="update 表名 set <列名>=<表达式> [where=<表达式>]" 2.sql=&q ...
- MongoDB学习笔记(2):数据库操作及CURD初步
MongoDB学习笔记(2):数据库操作及CURD 数据库操作 创建数据库 首先MongoDB中数据库的创建和数据库的切换都是使用命令,USE DATABASE,如果要切换的数据库不存在则会进行创建, ...
- 关于oracle批量插入数据遇到的问题
截取部分日志信息: 2015-09-01 14:48:47,132 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReade ...
- sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )
Annoying painting tool 题目描述 Maybe you wonder what an annoying painting tool is? First of all, the pa ...
- 青岛理工ACM交流赛 J题 数格子算面积
数格子算面积 Time Limit: 1000MS Memory limit: 262144K 题目描述 给你一个多边形(用’\’和’/’表示多边形的边),求多边形的面积. 输入 第一行两个正整数h ...