Codejam 2010 qualification round question C

这个大概勉强算是动态规划……吧。

 

'''
CodeJam Practice 
Created on 2013-01-28

@author: festony
'''

from cj_lib import *
from properties import *

import math

curr_file_name = 'C-large-practice'
#curr_file_name = 'C-small-practice'
#curr_file_name = 'test'


def input_dividing_func(input_lines):
    total_case = int(input_lines.pop(0))
    case_inputs = []
    for i in range(total_case):
        R, k, N = map(int, input_lines.pop(0).split(' '))
        g = map(int, input_lines.pop(0).split(' '))
        case_inputs.append([R, k, g])
    return case_inputs

def run_one_time(g, k, start_from, init_s = 0):
    l = len(g)
#    if start_from < 0:
#        start_from = l + start_from
    s = init_s
    hit_k = False
    for i in range(start_from, l):
        if s + g[i] <= k:
            s += g[i]
        else:
            hit_k = True
            break
    if hit_k:
        start_from = i
    else:
        if s + g[0] > k:
            hit_k = True
            start_from = 0
        else:
            s = init_s
    return hit_k, start_from, s

def full_run(g, k, start_from, limit_r = -1):
    l = len(g)
    sub_s = 0
    if start_from != 0:
        sub_s += sum(g[start_from:])
        start_from = 0
    S = 0
    r = 0
    hit_k, start_from, sub_s = run_one_time(g, k, start_from, sub_s)
    while hit_k and start_from > 0 and limit_r != r:
        S += sub_s
        sub_s = 0
        r += 1
        hit_k, start_from, sub_s = run_one_time(g, k, start_from, sub_s)
    if hit_k and limit_r != r:
        S += sub_s
        r += 1
    return r, start_from, S
    

def process_func(func_input):
    R, k, g = func_input
    if sum(g) <= k:
        return R * sum(g)
#    print full_run(g, k, 0, R)
    cache_start_from = []
    cache = dict()
    temp_R = R
    start_from = 0
    one_round_S = 0
    # find out start_from appearing period
    while not cache.has_key(start_from) and temp_R > 0:
        r, next_start_from, sub_s = full_run(g, k, start_from, temp_R)
        cache_start_from.append(start_from)
        cache[start_from] = [r, next_start_from, sub_s]
        start_from = next_start_from
        one_round_S += sub_s
        temp_R -= r
    # in the process maybe temp_R hit 0. in such case before we find out the period, the result is worked out.
    if temp_R == 0:
        return one_round_S
    S = one_round_S
    R = temp_R
    R_period = 0
    S_period = 0
    # after start_from has been found appearing periodically: we need to find the period, because the start_from value may not return to 0.
    # e.g. the start_from value may repeat as: 0, 5, 3, 2, 4, 3, 2, 4 ..... so the start_from is repeating 3, 2, 4, need to ignore the previous 0 and 5.
    hit_R = False
    for s in cache_start_from[cache_start_from.index(start_from, ):]:
        r, next_start_from, sub_s = cache[s]
        if R_period + r > R:
            hit_R = True
            break;
        R_period += r
        S_period += sub_s
    # and again, maybe work out the result before find the true period.
    if hit_R:
        R -= R_period
        S += S_period
        start_from = s
    else:
        S += S_period * (R / R_period)
        R %= R_period
        start_from = next_start_from
    while R > 0:
        r, next_start_from, sub_s = full_run(g, k, start_from, R)
        R -= r
        S += sub_s
        start_from = next_start_from
    return S

# to verify the result: this implementation is much slower but can ensure the results are correct.
def process_func2(func_input):
    R, k, g = func_input
    if sum(g) <= k:
        return R * sum(g)
    S = 0
    start_from = 0
    while R > 0:
        r, next_start_from, sub_s = full_run(g, k, start_from, R)
        R -= r
        S += sub_s
        start_from = next_start_from
    return S
    

run_proc(process_func, input_dividing_func, curr_working_folder, curr_file_name)
#run_proc(process_func2, input_dividing_func, curr_working_folder, curr_file_name)



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值