ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1029. Ministry

Can there be a negative number?
Posted by keivan 19 Nov 2010 16:49
I don't know why I get WA on test 4!
Can there be a negative number?
This is my code:

#include<iostream>
#include<cmath>
using namespace std;
long long dyn[110][510] , a[110][510] , big, sum[110][510] , p[110][510][2] , o , m, n;
int main(){
cin>>m>>n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
for(int i=0;i<m;i++)
{
sum[i][0]=a[i][0];
dyn[i][0]=100000000000000000;
for(int j=1;j<n;j++)
{
sum[i][j]=sum[i][j-1]+a[i][j];
dyn[i][j]=100000000000000000;
}
}
if((m==1)&&(n==1))
{
cout<<1<<endl;
return 0;
}
dyn[m-1][0]=a[m-1][0];
for(int i=1;i<n;i++)
{
dyn[m-1][i]=a[m-1][i];
}
for(int i=m-2;i>=0;i--)
{
for(int j=n-1;j>=0;j--)
{
for(int k=0;k<n;k++)
{
if(j>k)
{
if(dyn[i+1][k]+sum[i][j]-sum[i][k-1]<dyn[i][j])
{
dyn[i][j]=dyn[i+1][k]+sum[i][j]-sum[i][k-1];
p[i][j][0]=j;
p[i][j][1]=k;
}
}
else
{
if(dyn[i+1][k]+sum[i][k]-sum[i][j-1]<dyn[i][j])
{
dyn[i][j]=dyn[i+1][k]+sum[i][k]-sum[i][j-1];
p[i][j][0]=j;
p[i][j][1]=k;
}
}
}
}
}
big=dyn[0][0];
o=0;
for(int i=1;i<n;i++)
{
if(dyn[0][i]<big)
{
big=dyn[0][i];
o=i;
}
}
cout<<o+1;
for(int i=1;i<m-1;i++)
{
if(p[i][o][0]<=p[i][o][1])
{
for(int j=p[i][o][0];j<=p[i][o][1];j++)
cout<<" "<<j+1;
}
else
{
for(int j=p[i][o][0];j>=p[i][o][1];j--)
cout<<" "<<j+1;
}
o=p[i][o][1];
}
cout<<" "<<o+1<<endl;
return 0;
}
Re: Can there be a negative number?
Posted by keivan 20 Dec 2010 16:02
No there can't be.

My ac code.

[code deleted]

Edited by moderator 19.11.2019 23:27
Re: Can there be a negative number?
Posted by Alisher TATU 26 Apr 2011 22:32
thank you very much
Re: Can there be a negative number?
Posted by pmartynov 1 Nov 2013 21:11
Seems like your AC code fails at test:
5 5
10 1  10 10 10
1  1  10 10 10
1  10 1  1  1
1  1  1  10 1
10 10 10 10 1
Re: Can there be a negative number?
Posted by pmartynov 2 Nov 2013 18:05
I'm sorry. I should be more careful while reading the problem statement.
The correct answer here is 2 2 1 1 1 1.
Not 2 2 1 1 1 2 3 3 4 5 5 5.