Sicily8539(单调队列)


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

struct p
{
	int data;
	int pos;
}C[200005];
int a[200005];
int b[200005];

int main()
{
	int n;
	int i;
	int T=0;
	while (1)
	{
		T++;
		deque<struct p> Q;
		scanf("%d",&n);
		if (!n) break;
		for (i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for (i=1;i<=n;i++)
			scanf("%d",&b[i]);
		
		C[0].data=0;
		for (i=1;i<=n;i++)
		{
			C[i].data=C[i+n].data=C[i-1].data+a[i]-b[i];
			C[i].pos=i;
			C[i+n].pos=i+n;
		}
		
		printf("Case %d:",T);
		for (i=1;i<2*n;i++)
		{
		  while (!Q.empty() && Q.back().data>C[i].data)
				Q.pop_back();
			if (!Q.empty() && Q.front().pos<=i-n)
				Q.pop_front();
			Q.push_back(C[i]);
			if (i>=n)
			{
				int minn=Q.front().data;
				if (minn-C[i-n].data>=0)
					printf(" %d",i-n+1);
			}
		}
		printf("\n");
	}
}


你可能感兴趣的:(Sicily8539(单调队列))