您的位置:首页 > 其它

codeforce 66E - Petya and Post(dp+减少冗余)

2013-04-10 18:44 337 查看
E - Petya and Post
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
66E

Description

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 ai, bi 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 Input

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


思路:

D1=a1-b1,

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

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



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

x=min(D(i));如果x>=0那么从这个点走就一定OK。

现在走下一个点,就不需要从头到尾算上一遍了,

D2=a2-b2,

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



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

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

和前一次相比就是当前的值减去上一点的差值,除了D1例外,但由题意知这个点一定为0符合条件。

那么不就把前一次的最小值减去那个点的差值就是下一个点的最小值了吗???

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=1e5+9;
const int oo=1e9;
int a[mm],b[mm];
bool vis[mm];
int main()
{
int n;
while(cin>>n)
{ memset(vis,0,sizeof(vis));
for(int i=0;i<n;++i)cin>>a[i];
for(int i=0;i<n;++i)cin>>b[i];
int mi=oo,z=0,mmi=oo,zz=0;
for(int i=0;i<n;++i)
{z+=a[i]-b[i];zz+=a[n-i-1]-b[(n-i-2+n)%n];
mi=min(mi,z);mmi=min(mmi,zz);
}
int num=0;
for(int i=0;i<n;++i)
{
if(mi>=0&&!vis[i+1])vis[i+1]=1,++num;///找正方向
if(mmi>=0&&!vis[n-i])vis[n-i]=1,++num;///逆方向
mi-=a[i]-b[i];
mmi-=a[n-i-1]-b[(n-i-2+n)%n];
}
cout<<num<<"\n";
for(int i=0;i<=n;++i)
if(vis[i])
cout<<i<<" ";
cout<<"\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: