poj2566尺取变形
You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.
Input
Output
Sample Input
5 1
-10 -5 0 5 10
3
10 2
-9 8 -7 6 -5 4 -3 2 -1 0
5 11
15 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 100
0 0
Sample Output
5 4 4
5 2 8
9 1 1
15 1 15
15 1 15
题意:找连续的数使得和的绝对值和给定数差最小
题解:想了很久没想到,****因为尺取法必须单调数列才行,而且是求连续和****,先求前缀和,给其排序,再进行尺取
因为s不能等于t,当s==t时,t要++;sum>k时,向后移动s++,反之t++;
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int v,id;
}a[N];
int n; bool comp(const edge &a,const edge &b)
{
return a.v<b.v;
}
void solve(int x)
{
int s=,t=,ss,tt,sum,ans,minn=inf;
while(s<=n&&t<=n&&minn!=){
sum=a[t].v-a[s].v;
if(abs(sum-x)<minn)
{
minn=abs(sum-x);
ans=sum;
ss=a[s].id;
tt=a[t].id;
}
if(sum>x)s++;
else if(sum<x)t++;
else break;
if(t==s)t++;
}
if(ss>tt)swap(ss,tt);
cout<<ans<<" "<<ss+<<" "<<tt<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int k,s;
while(cin>>n>>k,n||k){
a[].v=a[].id=;
for(int i=;i<=n;i++)
{
int p;
cin>>p;
a[i].v=a[i-].v+p;
a[i].id=i;
}
sort(a,a+n+,comp);
while(k--){
cin>>s;
solve(s);
}
}
return ;
}
poj2566尺取变形的更多相关文章
- POJ:2566-Bound Found(尺取变形好题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...
- Gym 100703I---Endeavor for perfection(尺取)
题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- poj2100还是尺取
King George has recently decided that he would like to have a new design for the royal graveyard. Th ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- cf1121d 尺取
尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...
随机推荐
- linux虚拟机安装演示
做为一名linux初学者来说,在本机上安装linux虚拟机做为平时练习工具极为重要,此方式在充分体验linux操作乐趣的同时,又能保证不破坏已经配置好的服务器.所以虚拟机是一种很好的学习工具了.下面总 ...
- JavaScript重新介绍
本文转载自 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/A_re-introduction_to_JavaScript 引言 为什么 ...
- web从入门开始(5)-----表单
1. 表单的概念 是用来获取客户端用户数据的(信息)的.如:注册表单,查询表单,登录表单等. 2. 表单的工作原理 1.浏览有表单的网页,填写一些必要的信息,然后单击某个按钮,进行提交. 2.这 ...
- java中有符号和无符号数据类型发生转换
package com.itheima.test01;/* * byte short int long float double 是有符号位的数 * char boolean 是无符号位的数 * 补码 ...
- WINFROM 无边框窗体的移动和改变大小
因为去掉了边框 移动和调整大小都用不了了,可以调用WIN32的API来实现 1.定义必须常量 ; ; ; ; ; ; const int Guying_HTBOTTOMLEFT = 0x10; ; ...
- 集合框架(HashSet存储自定义对象保证元素唯一性)
HashSet如何保证元素唯一性的原理 1.HashSet原理 a. 我们使用Set集合都是需要去掉重复元素的, 如果在存储的时候逐个equals()比较, 效率较低,哈希算法提高了去重复的效率, 降 ...
- 关于window service2008系统iis部署访问证书,内部错误
近期因为在做微信支付系列,做到退款的时候需要通过把数据流通过证书post过去的时候,win7.win8.xp部署在iis都没问题.但是部署到服务器 2008的时候就出现了内部错误. 折腾许久,总算找到 ...
- Solr5.2.1+Zookeeper3.4.8分布式集群搭建
1.选取三台服务器 由于机器比较少,现将zookeeper和solr都部署在以下三台机器上.(以下操作都是在172.16.20.101主节点上进行的哦) 172.16.20.101 主节点 172.1 ...
- SaaS模式应用之多租户系统开发(单数据库多Schema设计)
SaaS是Software-as-a-Service(软件即服务)的简称,这边具体的解释不介绍. 多租户的系统可以应用这种模式的思想,将思想融入到系统的设计之中. 一.多租户的系统,目前在数据库存储上 ...
- 自适应滤波——线性预测(LPC)
作者:桂. 时间:2017-03-26 10:12:07 链接:http://www.cnblogs.com/xingshansi/p/6621914.html 声明:欢迎被转载,不过记得注明出处哦 ...