Monday, April 21, 2014

Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting solution

Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting: http://codeforces.com/contest/413/problem/B

Codeforces Coder-Strike 2014 - Round 2, problem: (B) Spyke Chatting solution: http://ideone.com/sd0vzI


Here is AC version: 
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0}, c[12]={0};

int main() {
    int n, m, k, x, y, r;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    for(int i=0; i<k; i++) scanf("%d%d", &x, &y), b[x-1]++, c[y-1]++;
    for(int i=0; i<n; i++) {
        r=0;
        for(int j=0; j<m; j++) if(a[i][j]) r+=c[j];
        r-=b[i];
        printf("%d ", r);
    }
    return 0;
}

note: you get every x and y to its array. because that sent message y from x affects every worker on that chat column, meaning a[i][j]=1 would get its share from y. that is why, we iterate over inner loop to notice the worker for every sent message if worker in that chatroom (for(int j=0; j<m; j++) if(a[i][j]) r+=c[j]; ) and then we subtract if that worker himself sent message (r-=b[i];  ).

Here is TLE version 1:
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0};

int main() {
    int n, m, k, x, y;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    while(k--) {
        scanf("%d%d", &x, &y);
        for(int i=0; i<n; i++) {
            if(i==x-1) continue;
            if(a[i][y-1]) b[i]++;
        }
    }
    for(int i=0; i<n; i++) printf("%d ", b[i]);
    return 0;
}


And here is TLE version 2:
#include <iostream>
#include <cstdio>
using namespace std;

const int ln=2*1e4;
int a[ln+1][12]={0}, b[ln+1]={0}, c[ln+1][12]={0};

int main() {
    int n, m, k, x, y;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]);
    while(k--) scanf("%d%d", &x, &y), c[x-1][y-1]++;
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(c[i][j]) {
        for(int p=0; p<n; p++) {
            if(p==i) continue;
            if(a[p][j]) b[p]+=c[i][j];
        }
    }
    for(int i=0; i<n; i++) printf("%d ", b[i]);
    return 0;
}

note on TLE: i think idea was correct, but brute force dont work as fast as its AC version above.

No comments:

Post a Comment