Saturday, January 3, 2015

Solution of 10401. Aliens at the train, Problem code: ALIEN ( SPOJ )

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

#define MAX 10000005
#define pf printf
#define sf scanf
typedef long long ll;
ll a[MAX];

int main()
{
    int i, l, test, start;
    ll sum, min_sum, st, max_st;
    ll A, B;
    scanf("%d", &test);

    while(test--)
    {
        sf("%lld%lld", &A, &B);
        for(i=1; i<=A; i++)
        {
             sf("%lld", &a[i]);
        }
        min_sum = 1000005;
        start = 1;
        max_st = 0;
        st = 0 ;
        st=0;
        sum = 0;
        for(i=1; i<=A; i++)
        {
            sum = sum + a[i];
            st = st + 1;
            if((max_st < st && sum <=B) || (max_st == st && min_sum > sum))
            {
                max_st = st;
                min_sum = sum;
            }
            while(sum>B)
            {
                sum = sum - a[start];
                st = st-1;
                start++;
            }
            if ((max_st < st && sum <=B) || (max_st == st && min_sum > sum))
            {
                max_st = st;
                min_sum = sum;
            }
        }
        pf("%lld %lld\n", min_sum, max_st);
    }
}



1 comment:

  1. What is Artificial Intelligence? Consider this excerpt from Tom Holt's novel "Almost Human": "The robot hesitated, while the Appeal Court of its mind pondered the nuances of the Laws of Robotics. Eventually they handed down a decision stating that the overriding law which supervened all others was that no robot shall say anything, no matter how true, that will inevitably earn it a smack in the mouth with a 5/8" Whitworth spanner. 'Sure thing, boss' it said"

    ReplyDelete

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...