Wednesday, April 16, 2014

Codechef April Challenge 2014, Long Contest ADIGIT - "Chef and Digits" solution

Codechef April Challenge 2014, Long Contest ADIGIT - "Chef and Digits": http://www.codechef.com/APRIL14/problems/ADIGIT

Codechef April Challenge 2014, Long Contest ADIGIT - "Chef and Digits" editorial: http://discuss.codechef.com/questions/41182/adigit-editorial

Codechef April Challenge 2014, Long Contest ADIGIT - "Chef and Digits" solution: 


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

int main() {
    int n, m, a[12], b[100005], x, tmp;
    char s[100005];
    for(int i=0; i<12; i++) a[i]=0;
    for(int i=0; i<100005; i++) b[i]=0;
    scanf("%d%d", &n, &m);
    scanf("%s", s);
    for(int i=0; i<n; i++) {
        tmp=s[i]-48;
        a[tmp]++;
        for(int j=0; j<10; j++) b[i]+=(max(j, tmp) - min(j, tmp))*a[j];
    }
    while(m--) {
        scanf("%d", &x);
        printf("%d\n", b[x-1]);
    }
    return 0;
}

No comments:

Post a Comment