POJ foot emulated

poj3061 Subsequence

Topic links: http://poj.org/problem?id=3061

Challenge P146. The meaning of problems: the given number of columns of length n integers a0, a1, ..., a (n-1), and the integer S, and then sum less than the minimum length of a continuous subsequence S, if the solution does not exist, the output to zero. $ 10 <n <10 ^ 5,0 <a_i <= 10 ^ 4, S <10 ^ 8 $;

Thinking: Size emulated, provided the starting subscript s, subscript e is turned off, and for the sum, initially s = e = 0; if sum <S, A will increase the sum (e), and e plus 1, if the sum > = S, TS update, and subtracting the sum a (s), sj 1 increases, and then comparing and S, this step is repeated, the code book is directly attached

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int x,a[100005];
    cin>>x;
    while(x--){
      int s=0,t=0,sum=0,S,n,ans;
      cin>>n>>S;
      ans=n+1;
      for(int i=0;i<n;i++)
        cin>>a[i];
      for(;;){
        while(t<n&&sum<S){
            sum+=a[t++];
        }
        if(sum<S)
            break;
        ans=min(ans,t-s);
        sum-=a[s++];
      }
      if(ans>n)
        cout<<0<<endl;
      else cout<<ans<<endl;

    }
}

poj2566 Bound Found

Topic links: http://poj.org/problem?id=2566

The meaning of problems: Given a sequence of length n and a non-negative integer t, find the sequence of a continuous sequence, so that the absolute value closest to and t, the output of the left and right index sequence;

This title is a "challenge" exercises on foot emulated part of the violence will certainly be T, do not start to feel emulated foot, looked at problem solution found himself still too much food, a little change under the shape will not ,,,,

Usually the number of columns emulated feet are ordered, to the number of columns is not ordered, as long as they obtain a prefix and then the discharge becomes ordered sequence, and a structure with z [i] stored prefix information and , z [i] .x i represents the number of the front and, since i vary sorted, so that z [i] .y recording initial i, can be used to update the nearest foot emulated subscript t is worth about sorted, should speak more clearly, oh -> _->

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n,k,a[100005];
struct Z{
int x;
int y;
}z[100005];
bool cmp(Z a,Z b){
return a.x<b.x;
}
int main(){
    while(scanf("%d%d",&n,&k)&&(n!=0||k!=0)){
          z[0].x=z[0].y=0;
          for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            z[i+1].x=z[i].x+a[i];
            z[i+1].y=i+1;
          }
    sort(z,z+n+1,cmp);
    for(int i=0;i<k;i++){
            int p;int s=0,t=1,temp,minn=0x3f3f3f3f,l,r,ans;
        scanf("%d",&p);
          while(s<=n&&t<=n){
             temp=z[t].x-z[s].x;
            if(abs(temp-p)<minn){
               minn=abs(temp-p);
               l=z[t].y;
               r=z[s].y;
               ans=temp;
            }
               if(temp>p)
                  s++;
               else if(temp<p)
                  t++;
               else break;
               if(s==t)
                 t++;
          }

    printf("%d %d %d\n",ans,min(l,r)+1,max(l,r));
    }

    }
}

poj2100 Graveyard Design

Topic links: http://poj.org/problem?id=2100

Meaning of the questions: Given a number n, ask whether there is a continuous sequence of natural numbers to make their sum of squares is n, according to a descending sequence length and the length of the output sequence of the entire sequence

Foot emulated, and poj3061 basically the same, because the output requirements, as long as standard with a structure length and starting the next record.

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct Z{
long long int x,y,z;
}z[1000005];
int main(){
long long int ans=0,S,s=1,t=1,sum=0;
cin>>S;
for(;;){
    while(sum<S){
        sum+=t*t;
        t++;
    }
    if(sum==S){
        z[ans].x=t-s;
        z[ans].y=s;
        z[ans].z=t-1;
        ans++;
    }
    sum-=s*s;
    s++;
    if(s*s>S)  break;
}
if(ans==0)
    cout<<0<<endl;
    else {
        cout<<ans<<endl;
        for(int i=0;i<ans;i++){
            cout<<z[i].x<<" ";
            int j;
            for( j=z[i].y;j<z[i].z;j++)
                cout<<j<<" ";
            cout<<j<<endl;
        }
    }
}

poj2739 Sum of Consecutive Prime Numbers

Topic links: http://poj.org/problem?id=2739

The meaning of problems: a known number n, (2 <= n <= 10000); if n can be expressed as a number of consecutive primes, then n is consecutive prime numbers, n asked how many representation;

Ideas: first number n within screening method to make a prime number table, and then judgment can be emulated foot;

#include<cstdio>
#include<iostream>
using namespace std;
int prime[10005],ans[10005]={0},is_prime[10005];
int main(){
 int p=0;
 for(int i=0;i<=10000;i++)
    is_prime[i]=1;
 is_prime[1]=is_prime[1]=0;
 for(int i=2;i<=10000;i++){
    if(is_prime[i]){
        prime[p++]=i;
        for(int j=2*i;j<=10000;j+=i)
            is_prime[j]=false;
    }
 }
 int s=0,t=0,sum=0;
 for(;;){
    while(t<10000&&sum<=10000){
        sum+=prime[t++];
        ans[sum]++;
    }
      sum=0;s++;t=s;
      if(prime[s]>10000)
         break;
  }
 int n;
 while((cin>>n)&&n!=0){
    cout<<ans[n]<<endl;
 }
}

  

  

 

Guess you like

Origin www.cnblogs.com/dlutjwh/p/10988144.html