编程介的小学生 2019-11-19 17:55 采纳率: 0.4%
浏览 123

Highway 是怎么写呢

Problem Description
As we all know, every day, there are hundreds of trucks passing through the highway. As some of the trucks might carry several tons of goods, the highway will be damaged unless it is frequently repaired. The administration of highway is worried about this, so it invented repairing cars to ensure that the cars can pass through the highway.

The highway has an initial durability. If a truck with x tons’ goods pass the highway, its durability will be decreased by x. Once the highway’s durability is less or equal to zero, it will be broken and can never be repaired. The trucks can’t pass through the broken ones.

There are two kinds of repairing cars: T1 can increase the highway’s durability by r, T2 can increase the highway’s durability to p, if the highway’s durability is less than p. Although the repairing cars can pass through the broken parts, the broken parts can’t be repaired.

Input
The input consists of several test cases.

For every test case, there are three integers N (1<=N<=100000), M (1<=M<=100000), I (1<=I<=1000) in the first line, indicating the highway’s length, the numbers of cars and the initial durability of the highway.

Each of the next M lines described the information of cars in the following format:
1 s t d -- There is a truck with d tons’ goods wanted to pass the interval [s, t]. You should check whether the truck can pass it. Notice that if the truck can't pass the whole interval, it will give up the whole passing; otherwise it can pass the highway freely, even if the highway will be broken after the truck’s passing.
2 s t r -- A T1 car will pass the interval [s, t] and increase its durability by r.
3 s t p -- A T2 car will pass the interval [s, t] and increase its durability to p.
You can assume that 1<=s<=t<=N, 1<=d, p, r<=1000

The input ends with N=M=I=0.

Output
For each case, you should return how many trucks can successfully pass the interval.

Sample Input
5 5 5
1 1 3 3
2 2 3 10
1 1 3 3
1 1 3 1
1 2 3 1
5 3 10
1 1 2 5
1 2 3 5
1 1 3 5
0 0 0

Sample Output
3
2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 13:34
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # read data
    data <- read.table(text = "<stdin>")
    
    # define function
    def_pass <- function(s, t, dur, type) {
        # calculate the remaining durability
        remain_dur <- dur - (t - s)
        
        # determine which repair car to use
        if(type == "T1") {
            if(remain_dur >= 0) {
                # increase durability by T1
                remain_dur <- remain_dur + 1
            } else {
                # no repair needed
                remain_dur <- 0
            }
        } else {
            if(remain_dur < p) {
                # no repair needed
                remain_dur <- 0
            } else {
                # increase durability by T2
                remain_dur <- min(p, remain_dur)
            }
        }
        
        # check if the remaining durability is still positive
        if(remain_dur > 0) {
            # the truck can pass the interval
            return(c(s, t))
        } else {
            # the truck cannot pass the interval
            return(NULL)
        }
    }
    
    # main program
    n <- nrow(data)
    m <- ncol(data)
    
    result <- numeric(n)
    for(i in seq_along(result)) {
        result[i] <- sum(sapply(1:n, function(j) def_pass(data[,i,j], data[,j,i+1], data[,i,j+1], data[,i,j+2])))
    }
    
    write.table(result, file = "<stdin>")
    
    评论

报告相同问题?