1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <vector>
#define MAX_SIZE_TIME 15
#define MAX_NUM_TIMES 200000

typedef long long int_t;

int_t get_min_sum(std::map<int_t, int> *diff_map, int_t oven_time) {
    int_t sum = 0;
    for (std::map<int_t, int>::iterator it=diff_map->begin(); it!=diff_map->end(); ++it)
    {
        int_t val = it->first;
        int mult = it->second;
        int_t diff = val - oven_time;
        if(diff < 0) {
            sum += mult * (-1) * diff;
        }
    }
    return sum;

}

void compute_min_times(char* buffer, std::map<int_t, int> *diff_map, std::vector<int_t> *ovens)
{
  int offset = 0;
  for(std::vector<int_t>::iterator it = ovens->begin(); it != ovens->end(); ++it)
  {
    offset += sprintf(buffer+offset, "%lld\n", get_min_sum(diff_map, *it));
  }
}

void update_diff_map(std::map<int_t, int> *diff_map, int_t diff)
{
    std::map<int_t, int>::iterator it = diff_map->find(diff);
    int val = 1;
    if(it != diff_map->end())
    {
        val += it->second;
        it->second = val;
    }
    else {
        diff_map->insert(std::pair<int_t, int>(diff, val));
    }

}
char* get_next_num(const char* input, int_t *val) {
    char arr[21];
    char* any;
    int i = 0;
    char c;
    c = input[i];
    *val = 0;
    while(c >= '0' && c <= '9')
    {
        *val = (*val) * 10;
        arr[i] = c;
        i++;
        *val += (int_t)(c - '0');
        c = input[i];
    }

    arr[i] = '\0';
   // *val = strtoull(arr, &any, 10);
    if(c == '\0')
    {
        return NULL;
    }
    else {
        return (char*)input + i + 1;
    }
}

void process_input(char* input_line, size_t input_buffer_size, std::map<int_t, int> *t_map, std::vector<int_t> *ovens) {
    memset(input_line, 0, input_buffer_size);
    gets(input_line);
    int_t n,m;
    char* next = get_next_num(input_line, &n);
    get_next_num(next, &m);
    memset(input_line, 0, input_buffer_size);
    gets(input_line);
    int i = 0;
    next = input_line;
    int_t time;
    int_t prev_time = 0;

    for(i = 0; (i < n && next != NULL); i++) {
        time = 0;
        next = get_next_num(next, &time);
        int_t diff = time - prev_time;
        prev_time =  time;
        update_diff_map(t_map, diff);
    }
    memset(input_line, 0, input_buffer_size);
    gets(input_line);
    next = input_line;
    int_t oven_time;
    for(i = 0; (i < m && next != NULL); i++) {
        oven_time = 0;
        next = get_next_num(next, &oven_time);
        ovens->push_back(oven_time);
    }
}

void gen_input() {
        int_t m,n;
    m = 200000;
    n = 200000;
    printf("%lld %lld\n", n, m);
    int i = 0;
    int_t time_val = 1000000000000l;
    int_t oven_time_val = 1000000000000l;
    for(i = 1; i < n; i++) {
        printf("%lld ", time_val);
    }
    printf("%lld\n", time_val);

    for(i = 1; i < m; i++) {
        printf("%lld ", oven_time_val);
    }
    printf("%lld\n", oven_time_val);

}

int main(int argc, char* argv[]) {
    size_t input_buffer_size = MAX_SIZE_TIME*MAX_NUM_TIMES;
    char* input_line = (char*) malloc(input_buffer_size);

    std::map<int_t, int>  times_map;
    std::vector<int_t> ovens;
    process_input(input_line, input_buffer_size, &times_map, &ovens);

    memset(input_line, 0, input_buffer_size);
    compute_min_times(input_line, &times_map, &ovens);
    printf("%s", input_line);
    free(input_line);
    return 0;
}