Cod sursa(job #288926)

Utilizator AndreyPAndrei Poenaru AndreyP Data 26 martie 2009 11:13:10
Problema Cuburi2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include<stdio.h>
#define N 250010
int n,m;
int v[N];
long long s[N],ajt[N];
long long sr[N],ajtr[N];
char c[9*N];
inline void citescv()
{
	fgets(c,9*N,stdin);
	int cate=0,aux=0;
	for(int i=0; c[i]!='\0'; ++i)
	{
		if(c[i]>='0' && c[i]<='9')
			aux=aux*10+c[i]-'0';
		else
		{
			v[++cate]=aux;
			aux=0;
		}
	}
	if(cate<n)
		v[++cate]=aux;
}
inline void citire()
{
	scanf("%d%d\n",&n,&m);
	citescv();
}
inline void precalc()
{
	for(int i=1; i<=n; ++i)
	{
		long long aux=(long long)v[i];
		s[i]=s[i-1]+aux;
		ajt[i]=ajt[i-1]+s[i];
	}
	for(int i=n; i; --i)
	{
		sr[i]=sr[i+1]+v[i];
		ajtr[i]=ajtr[i+1]+sr[i];
	}
}
inline long long coststg(int x,int y)
{
	if(x>y)
		return 0;
	return ajt[y]-ajt[x-1]-s[x-1]*(y-x+1);
}
inline long long costdr(int x,int y)
{
	if(x>y)
		return 0;
	return ajtr[x]-ajtr[y+1]-sr[y+1]*(y-x+1);
}
inline void rezolva()
{
	char c[30];
	fgets(c,30,stdin);
	int x=0,y=0,i=0;
	for(i=0; c[i]>='0' && c[i]<='9'; ++i)
		x=x*10+c[i]-'0';
	for(++i; c[i]>='0' && c[i]<='9'; ++i)
		y=y*10+c[i]-'0';
	if(x==y)
	{
		printf("%d 0\n",x);
		return;
	}
	int p=y;
	long long cost;
	long long cost1;
	int u=y;
	p=x;
	int pok=x;
	/*while(p<=u)
	{
		int m=(p+u)>>1;
		long long aux1=coststg(x,m-1);
		long long aux2=costdr(m+1,y);
		if(aux2-aux1>0)
		{
			pok=m;
			p=m+1;
		}
		else
			u=m-1;
	}
	p=pok;
	cost=coststg(x,pok-1)+costdr(pok+1,y);
	if(pok<y)
	{
		p=pok+1;
		cost1=coststg(x,p-1)+costdr(p+1,y);
		if(cost1<cost)
		{
			cost=cost1;
			pok=p;
		}
	}
	printf("%d %lld\n",pok,cost);*/
	cost=costdr(x+1,y);
	p=x;
	cost1=coststg(x,y-1);
	if(cost1<cost)
	{
		cost=cost1;
		p=y;
	}
	for(int i=x+1; i<y; ++i)
	{
		cost1=coststg(x,i-1)+costdr(i+1,y);
		if(cost1<cost)
		{
			cost=cost1;
			p=i;
		}
	}
	printf("%d %lld\n",p,cost);
}
int main()
{
	freopen("cuburi2.in","r",stdin);
	freopen("cuburi2.out","w",stdout);
	citire();
	precalc();
	for(; m; --m)
		rezolva();
	return 0;
}