Codeforces 66E Petya and Post

E. Petya and Post
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations.

The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers.

Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it?

Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers aibi is no less than 1 and no more than 109.

Output

Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.

Sample test(s)
input
4
1 7 2 3
8 1 1 3
output
2
2 4
input
8
1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1
output
8
1 2 3 4 5 6 7 8

--------------------------------------------------------------------------------------------------------------------------

First of all we divide our problem into 2 parts: consider stations from which we can start if we are moving in the clockwise direction and stations from which we can start if we are moving in the counterclockwise direction.

Obviously, if we know the solution of one of these problems, we know the solution of another problem.

So, we may assume that stations are located in the counterclockwise order and we are moving in the counterclockwise direction.

Consider the following differences:

D1=a1-b1,

D2=(a1+a2)-(b1+b2),

D3=(a1+a2+a3)-(b1+b2+b3),

Dn=(a1+a2+…+an)-(b1+b2+…+bn);

Obviously if one of Di’s is less than a zero, then we cannot drive one round along the road. Let D = min(Di) – we will use it later.

Obviously, if D<0 then the first station cannot be the start station.

Now, we can check with complexity O(n) whether the first station can be used as the starting point. Next, we want to show how we can check this for the second station with complexity O(1).
To show this, consider:

E1=D1-(a1-b1),

E2=D2-(a1-b1),

En=Dn-(a1-b1).

Next, substitute Di in these equalities. We get the following:

E1=(a1-b1)-(a1-b1)=0=(a2+a3+…+an+a1)-(b2+b3+…+bn+b1) – (a1+…+an=b1+…+bn=X)

E2=(a1+a2)-(b1+b2)-(a1-b1)=a2-b2

E3=(a1+a2+a3)-(b1+b2+b3)-(a1-b1)=(a2+a3)-(b2+b3)

En=(a1+a2+…+an)-(b1+b2+…+bn)-(a1-b1)=(a2+…+an)-(b2+…+bn)

But it’s easy to see that number E1 has the same meaning for the second station as number D1 for the first one. So, we just have to check min(Ei)>=0. But Ei=Di-(a1-b1), so we have to check min(Di-(a1-b1))>=0. Now, we can see that if min(Di)=Dk, then min(Di-(a1-b1))=Dk-(a1-b1). So, if we know Dk, that we can check whether the second station can be the starting point with complexity O(1). Similarly, we can check this for the third, the fourth, …, the nth stations.

Now we should check the same things but assuming that the car is moving in the clockwise direction.

--------------------------------------------------------------------------------------------------------------------------

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

const int maxn=111111;
const int OO=1e9;

int gas[maxn],dis[maxn];
bool v[maxn];
int n,Dk,sum,ans;

int main() {
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&gas[i]);
    for(int i=0;i<n;i++) scanf("%d",&dis[i]);

    Dk=OO;
    sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=gas[i]-dis[i];
        Dk=min(Dk,sum);
    }

    ans=0;
    for(int i=0;i<n;i++)
    {
        if(Dk>=0 && !v[i])
        {
            v[i] = true;
            ans++;
        }
        Dk-=gas[i]-dis[i];
    }

    Dk=OO;
    sum=0;
    for(int i=n-1;i>=0;i--)
    {
        sum+=gas[i]-dis[(i-1+n)%n];
        Dk=min(Dk,sum);
    }

    for(int i=n-1;i>=0;i--)
    {
        if(Dk >= 0 && !v[i])
        {
            v[i]=true;
            ans++;
        }
        Dk-=gas[i]-dis[(i-1+n)%n];
    }

    printf("%d\n", ans);
    for(int i=0;i<n;i++)
    {
        if(v[i])
        {
            printf("%d ", i+1);
        }
    }
    printf("\n");
    return 0;
}




原文地址:https://www.cnblogs.com/cyendra/p/3038445.html