uva 11951 - Area Solution O(n^3)
uva 11951 - Area
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
using namespace std;
void maxSum(const int *const arr,int len,int max,int &count,int &lowest_cost){
int i,j,sum=0;
count=0;
lowest_cost=0;
for(i=0;i<len;i++){
sum=arr[i];
if(sum<=max){
for(j=i+1;j<len;j++){
sum=sum+arr[j];
if(sum>max){
sum=sum-arr[j];
break;
}
}
if((j-i)>count){
count=j-i;
lowest_cost=sum;
}else if((j-i)==count && lowest_cost>sum){
lowest_cost=sum;
}
}
}
}
int main()
{
int arr[105][105];
int lowest,count,temp_count,temp[105],i,j,n,m,k,final_lowest,test_case,max,test;
cin>>test_case;
test=1;
while(test_case--){
cin>>n>>m>>max;
final_lowest=0;
count=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>arr[i][j];
for(i=0;i<n;i++){
fill(temp,temp+m,0);
for(j=i;j<n;j++){
for(k=0;k<m;k++){
temp[k]=temp[k]+arr[j][k];
}
//temp_count=0;
maxSum(temp,m,max,temp_count,lowest);
temp_count=(j-i+1)*temp_count;
if(temp_count>count){
final_lowest=lowest;
count=temp_count;
}else if(temp_count==count && final_lowest>lowest){
final_lowest=lowest;
}
}
}
cout<<"Case #"<<test++<<": "<<count<<" "<<final_lowest<<endl;
}
return 0;
}
Comments
Post a Comment