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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)

發布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

給定一個數組和一個值t,求一個子區間使得其和的絕對值與t的差值最小,如果存在多個,任意解都可行。

題目:

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

分析:

1、 什么情況下能使用尺取法?

尺取法通常適用于選取區間有一定規律,或者說所選取的區間有一定的變化趨勢的情況,通俗地說,在對所選取區間進行判斷之后,我們可以明確如何進一步有方向地推進區間端點以求解滿足條件的區間,如果已經判斷了目前所選取的區間,但卻無法確定所要求解的區間如何進一步得到根據其端點得到,那么尺取法便是不可行的。。首先,明確題目所需要求解的量之后,區間左右端點一般從最整個數組的起點開始,之后判斷區間是否符合條件在根據實際情況變化區間的端點求解答案。

摘自博客

(1)我在補挑戰程序設計,所以明確知道這道題使用尺取法,但我對于 問題分析后,完全沒看出來選取區間有啥規律(由于題目是求一個子區間使得其和的絕對值與t的差值最小,如果存在多個,任意解都可行。存在負數)所以對于區間端點的推進是無法正常進行的。
(2)對前綴和sum排序。然后這時候的sum就有單調性了,根據每次兩個區間端點作差的值與目標值比較,對端點嘗試更新。
(3)原問題就轉化為了在一個升序區間中,取兩個區間端點作差,求最接近 s 的值。
(4)需要care:
1、當左右端點相等時,此時為前綴和,沒有意義且不可能出現,導致錯誤,所以我們要排除。
2、sort排序要帶上起始的初始化零點,一是答案可能導致錯誤,二一個我們是用前綴和進行比較,所以不可或缺。

AC模板:

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int inf=0x3f3f3f3f; const int M=1e5+10; int n,m,k,l,r,x,y,ans,mi; struct node {int id,sum; }s[M]; bool cmp(node x,node y) {return x.sum<y.sum; } void solve() {l=0,r=1,mi=inf;while(l<=n&&r<=n&&mi!=0){int temp=s[r].sum-s[l].sum;if(abs(temp-k)<mi){mi=abs(temp-k);x=s[l].id;y=s[r].id;ans=temp;}if(temp>k) l++;else if(temp<k) r++;else break;if(r==l) r++;}if(x>y) swap(x,y);printf("%d %d %d\n",ans,x+1,y); } int main() {while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;s[0].sum=s[0].id=0;for(int i=1;i<=n;i++){scanf("%d",&s[i].sum);s[i].sum+=s[i-1].sum;s[i].id=i;}sort(s,s+1+n,cmp);/**要帶上0項*/while(m--){scanf("%d",&k);solve();}}return 0; }

備戰ccpc分站賽ing ,題目分析簡略,見諒,轉載請注明出處。。。。。

總結

以上是生活随笔為你收集整理的Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。