日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Bound Found POJ - 2566(尺取法)

發(fā)布時(shí)間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bound Found POJ - 2566(尺取法) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: “But I want to use feet, not meters!”). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We’ll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.

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
The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n=k=0. Otherwise, 1<=n<=100000 and there follow n integers with absolute values <=10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0<=t<=1000000000.
Output
For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.
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
題意:找出一段連續(xù)的序列,使得這個(gè)序列的和的絕對(duì)值和t的差值最小。輸出這段區(qū)間的絕對(duì)值和區(qū)間的左右端點(diǎn)。
思路:尺取的前提條件就是序列必須是單調(diào)的,否則前進(jìn)和后退根本找不出規(guī)律來,就沒有辦法尺取了。但是如果堆這個(gè)數(shù)組排序的話,就不能保證這是連續(xù)的了。我們先把這段序列的前綴和數(shù)組求出來,然后將前綴和出現(xiàn)的序列號(hào)一同保存下來,按照由小到大排序。這樣序列就是單調(diào)的了。然后兩個(gè)指針開始尺取,如果當(dāng)前區(qū)間求出的和小于t的話,說明還可以變大一點(diǎn),那么就r++;如果當(dāng)前區(qū)間求出的和大于t的話,說明還可以變小一點(diǎn),那么就l++。注意這是前綴和,l和r不能相同。
代碼如下:

#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #define ll long long #define inf 0x3f3f3f3f using namespace std;const int maxx=1e5+100; struct node{int num;int pos;bool operator<(const node &a)const{return num<a.num;} }p[maxx]; int a[maxx]; int n,m;int main() {while(~scanf("%d%d",&n,&m)&&(n+m)){p[0].num=0;p[0].pos=0;//這里需要解釋一下,因?yàn)檫@是前綴和,不是原來的數(shù)組,添加一個(gè)0是因?yàn)橛锌赡軓念^開始取元素,那樣的話,p[l].num需要為0才可以。for(int i=1;i<=n;i++) {scanf("%d",&a[i]);p[i].num=p[i-1].num+a[i];p[i].pos=i;}sort(p,p+1+n);//從0開始排序。while(m--){int t;scanf("%d",&t);int ans=inf,Ans;int L=0,R=0;int l=0,r=1;while(r<=n){int res=p[r].num-p[l].num;if(abs(res-t)<ans){ans=abs(res-t);Ans=res;L=p[l].pos;R=p[r].pos;}if(res<t) r++;else if(res>t) l++;else break;if(l==r) r++;}if(L>R) swap(L,R);printf("%d %d %d\n",Ans,L+1,R);}}return 0; }

尺取就是需要多思考。
努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Bound Found POJ - 2566(尺取法)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。