Pagini recente » Cod sursa (job #1616609) | Cod sursa (job #2506393) | Cod sursa (job #1142730) | Cod sursa (job #783138) | Cod sursa (job #288904)
Cod sursa(job #288904)
#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("x 0\n");
return;
}
int p=y;
long long cost;
long long aux=(long long)(y-x);
cost=ajt[y]-ajt[x]-aux*s[x-1];
long long cost1=cost;
int u=y;
p=x;
long long dif=1LL<<62;
while(p<u)
{
int m=(p+u)>>1;
long long aux1=coststg(x,m-1);
long long aux2=costdr(m+1,y);
if(aux1<=aux2 && aux2-aux1<dif)
{
u=m;
dif=aux2-aux1;
}
else
p=m+1;
}
cost=coststg(x,p-1)+costdr(p+1,y);
u=p;
if(p!=x)
{
cost1=coststg(x,p-2)+costdr(p,y);
if(cost1<cost)
{
cost=cost1;
u=p-1;
}
}
if(p!=y)
{
cost1=coststg(x,p)+costdr(p+2,y);
if(cost1<cost)
{
cost=cost1;
u=p+1;
}
}
printf("%d %lld\n",u,cost);
}
int main()
{
freopen("cuburi2.in","r",stdin);
freopen("cuburi2.out","w",stdout);
citire();
precalc();
for(; m; --m)
rezolva();
return 0;
}