Monday, March 17, 2014

Codeforces Round #236 (Div. 2), problem: (B) Trees in a Row solution

Codeforces Round #236 (Div. 2), problem: (B) Trees in a Row: http://codeforces.com/contest/402/problem/B

Codeforces Round #236 (Div. 2), problem: (B) Trees in a Row solution: http://ideone.com/0vO1gQ

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

int main() {
    int n, k, a[1005], cnt, no, min;
    min=INT_MAX;
    scanf("%d%d", &n, &k);
    for(int i=0; i<n; i++) scanf("%d", &a[i]);
    for(int i=1; i<1005; i++) {
        cnt=0;
        for(int j=0; j<n; j++) if(a[j]!=i+k*j) cnt++;
        if(cnt<min) min=cnt, no=i; 
    }
    printf("%d\n", min);
    for(int i=0; i<n; i++) {
        if(a[i]>no+k*i) printf("- %d %d\n", i+1, a[i]-(no+k*i));
        else if(a[i]<no+k*i) printf("+ %d %d\n", i+1, (no+k*i)-a[i]);
    }
    return 0;
}

note: since constraint is (1 ≤ ai ≤ 1000) so just take every possible size, and take the ideal one - the minimum number of minutes the gardener needs. 

No comments:

Post a Comment