Friday, January 31, 2014

codechef ROWCOLOP - "row and column operations" solution

codechef ROWCOLOP - "row and column operations" :  http://www.codechef.com/problems/ROWCOLOP/
codechef ROWCOLOP - "row and column operations" editorial :  http://discuss.codechef.com/questions/6643/rowcolop-editorial

codechef ROWCOLOP - "row and column operations" draft : http://ideone.com/sJiw9Z
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
int n, q, r[314160], c[314160], max[314160], ans, b, d;
char s[10];
ans=0;
memset(r, 0, sizeof(r));
memset(c, 0, sizeof(c));
memset(max, 0, sizeof(max));
scanf("%d%d", &n, &q);
for(int i=0; i<q; i++) {
scanf("%s%d%d", s, &b, &d);
if(s[0]=='R') c[b-1]+=d;
else r[b-1]+=d;
}
for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(r[i]+c[j]>max[i]) {
max[i]=r[i]+c[j];
if(max[i]>ans) ans=max[i];
}
printf("%d", ans);
return 0;
}

codechef ROWCOLOP - "row and column operations" solution :  http://ideone.com/GRdwpH

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

int main() {
int n, q, r[314160], c[314160], mc, mr, b, d;
char s[10];
memset(r, 0, sizeof(r));
memset(c, 0, sizeof(c));
mc=-1;
mr=-1;
scanf("%d%d", &n, &q);
for(int i=0; i<q; i++) {
scanf("%s%d%d", s, &b, &d); 
if(s[0]=='R') c[b-1]+=d;
else r[b-1]+=d;
}
for(int i=0; i<n; i++) if(r[i]>mr) mr=r[i];
for(int i=0; i<n; i++) if(c[i]>mc) mc=c[i];
printf("%d\n", mc+mr);
return 0;
}

note: 2-d array with that large number is impossible. so instead lets have two 1-d array, for columns and rows. when "rowadd" we increase our column array's row index indicated at "rowadd". we apply the same to our row array. by doing so, we now know exact amount of our imaginary array[i][j] by looking at our column array r[i] and row array c[j]. so, maximum number of array[][] will be also, max r[] + max c[]. my draft gives TLE, i posted my draft so that by looking at my draft you can easily get the point.

No comments:

Post a Comment