首页 > 试题广场 >

罪犯转移

[编程题]罪犯转移
  • 热度指数:40453 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式(一组测试用例可能包含多组数据,请注意处理)?

输入描述:
第一行数据三个整数:n,t,c(1≤n≤2e5,0≤t≤1e9,1≤c≤n),第二行按入狱时间给出每个犯人的罪行值ai(0≤ai≤1e9)


输出描述:
一行输出答案。
示例1

输入

3 100 2
1 2 3

输出

2
arr=input("")
z=[int(n) for n in arr.split()] 
brr=input("")
a=[int(i) for i in brr.split()] 
k=0
for i in range(0,z[0]-z[2]+1):
    if sum(a[i:i+z[2]])<=z[1]:
        
        k+=1
        
print(k)
发表于 2020-08-19 15:17:49 回复(0)
while True:
    try:
        n, t, c = map(int, input().split())
        a = list(map(int, input().split()))
        temp_sum = sum(a[:c])
        res = 1 if temp_sum <= t else 0
        for i in range(n - c):
            temp_sum += a[i + c] - a[i]
            if temp_sum <= t:
                res += 1
        print(res)
    except Exception:
        break
编辑于 2019-05-05 23:57:01 回复(0)
if __name__ == "__main__":
    while True:
        try:
            tp1 = input()
            tp2 = input()
            n, t, c = map(int, tp1.split())
            a = [float(x) for x in tp2.split()]
            f = sum(a[:c])
            count = 1 if f <= t else 0
            i = c
            while i < n:
                f = f - a[i-c] + a[i]
                if f <= t:
                    count += 1
                i += 1
            print(count)
        except:
            break

发表于 2019-03-15 18:15:06 回复(0)
while True:
    try:
        n,t,c=list(map(int,input().split()))
        a=list(map(int,input().split()))
    except:
        break
    prisoner=a[:c]#选定罪犯,初始为a[0]~a[c-1]
    sums=sum(prisoner)#罪行值之和
    if sums>t:
        count=0
    else:#不超过t则计数+1
        count=1
    for i in range(c,n):#从a[c]开始,轮流加进去新的犯人a[i],并把最开始的犯人a[i-c]去除
        prisoner.append(a[i])
        prisoner.pop(0)
        sums=sums+a[i]-a[i-c]#更新罪行值之和
        if sums<=t:
            count+=1
    print(count)

发表于 2018-07-18 15:27:04 回复(0)
while True:  
    try:
        n, t, c = raw_input().split()
        n = long(n)
        t = long(t)
        c = long(c)
        if n < 1 or n > 2e5:
            break
        elif t < 0 or t > 1e9:
            break
        elif c < 1 or c > n:
            break
        else:
            pass
        aa = raw_input().split()
        
        a = [long(x) for x in aa]
        sumx = 0
        num = 0
        for i in range(c):
            sumx += a[i]
        i = c
        if sumx <= t:
            num += 1
        
        while i < n:
            start = i - c
            stop = i
            sumx -= (a[start] - a[stop])
            if sumx <= t:
                num += 1
            i += 1
        print num
    except EOFError, e:
        break


发表于 2017-08-30 22:12:07 回复(0)
import sys

def getsolution(n,t,c,a):
    count=0
    cursum=0
    for j in range(c):
        cursum+=a[j]
    for i in range(len(a)):
        if i>len(a)-c:
            break
        if i>0:
            cursum-=a[i-1]
            cursum+=a[i+c-1]

        if cursum<=t:
            count+=1
    return count
 
all=[]
for line in sys.stdin:
    all.append(line.strip())

for i in range(0,len(all),2):
    n,t,c=[int(x) for x in all[i].split(' ')]
    a=[int(x) for x in all[i+1].split(' ')]
    print getsolution(n,t,c,a)
发表于 2016-08-03 20:51:46 回复(0)
import sys
a = raw_input().strip()
n,t,c = [int(i) for i in a.split()]
b = raw_input().strip()

aa = [int(i) for i in b.split()]
total = sum(aa[:c])
res = 0
if total <= c:
    res+=1
for idx in range(0, len(aa)-c):
    total = total - aa[idx] + aa[idx+c]
    if total <= c:
        res+=1
sys.stdout.write(str(res))

pyton 代码如上, 错误如下,请问在哪里出错啦


答案错误:您提交的程序没有通过所有的测试用例

case通过率为0.00%
测试用例:
86530 3687600 26340

对应输出应该为:

60191

你的输出为:

0
发表于 2016-08-02 22:32:38 回复(0)

问题信息

难度:
7条回答 32856浏览

热门推荐

通过挑战的用户

查看代码