HDU 1422 annular maximal nonnegative sub-segment

Source: Internet
Author: User

Test instructions
    • A number of columns that form a ring to find the maximum length of the sub-segments where each step is greater than or equal to 0
Solution:
  • Similar rings are used two arrays to achieve the way, but look at others code found not necessary, open more space, directly to the subscript to take the surplus operation can achieve the ideal effect.
  • I am the starting point of the enumeration ring (from 0 to n-1), and then each starting point is a sequence of n, and the maximum substring length is calculated with a method similar to the maximal continuous substring, so that the complexity is o ( < Span style= "Position:absolute; Clip:rect (2.029em 1000.003em 2.776em-0.477em); Top: -2.611em; Left:0.003em; " >n 2 )
  • There is a better way of thinking: you put this twice times the length of the sequence (0 to 2n-1), to find the longest to meet the conditions of the sub-segment (note that the length of the sub-paragraph to exit, or else error), this idea does not control a sequence length of n constraints, Actually only this entire sequence satisfies the condition is the length is n the error and calculates the result of 2n, here the preceding one achieves N to exit can be specially solved.
My Code:
#include <queue>#include <set>#include <map>#include <stack>#include <string>#include <vector>#include <algorithm>#include <cstdio>#include <cstring>#include <iostream>using namespace STD;//HDU 1422//problems//typedef Long Long intLL;Const intM =1000009, INF =0x3fffffff;intN, A[m *2], Dp[m], key, ans;intMainvoid) { while(~scanf("%d", &n)) {memset(DP,0,sizeof(DP)); Ans = key = a[0] =0; dp[0] =1; for(inti =1; I <= N; i++) {intx, y;scanf("%d%d", &x, &y);        A[i] = a[i + N] = x-y; } for(inti =1; I <= N; i++) {if(Dp[i-1] >0) {Key-= a[i-1]; Dp[i] = dp[i-1] -1; }if(Key <0) { for(intj = i + Dp[i]-1; ;                    j--) {Key-= a[j]; dp[i]--;if(Key >=0) Break; }            }Else{ for(intj = i + dp[i]; J < i + N; J + +) {if(Key + A[j] <0) Break;                    Key + = A[j];                dp[i]++; }            }if(Dp[i] > ans) ans = dp[i];if(ans = = N) Break; }printf("%d\n", ans); }return 0;}
Better code:
#include <stdio.h>intMain () {inti,n,a,b,sum,cnt,max,ar[100005]; while(SCANF ("%d", &n)!=eof) { for(i=0; i<n;i++) {scanf ("%d%d", &a,&b);        Ar[i]=a-b; } sum=cnt=0; max=-1; for(i=0;i<2*n-1; i++) {Sum+=ar[i%n];if(sum>=0) {cnt++;if(max<cnt) max=cnt;if(cnt==n) Break; }ElseSum=cnt=0; }printf("%d\ n", Max); }return 0;}

HDU 1422 annular maximal nonnegative sub-segment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.