列表

详情


NC212615. CookSteak

描述

The party began, the greasy uncle was playing cards, the fat otaku was eating, and the little beauty was drawing.

After playing cards for a while, ZJH felt very hungry. He decided to let the automatic kitchen grill steak for him. It can make the kitchen quite busy, because ZJH tastes food very strictly, especially steak!

To grilling a steak, ZJH believes that it required N grilling processes in order, each function within a temperature range for one minute, only in this case the grilled steak is the best.

Fortunately, the device in the kitchen has already been equipped with artificial intelligence, which can quickly complete the whole process in the shortest time. The temperature of this device can be increased or decreased by one degree per minute. When the temperature is within the required temperature range, the steak will be grilled for one minute. However, the trouble now is that ZJH forgot to write a program to calculate the completion time of the steak when remodelling the automatic kitchen system, so you are asked to add this program to the system.

Given an integer N, indicating the number of processes, and N pairs of numbers , representing the temperature range required by each function. The initial temperature of the device is 0. Your program should tell ZJH how long it takes for each steak to be grilled.

输入描述

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases(number of steaks ZJH wants to eat!). For each test case:

The first line contains an integer N (1 ≤ N ≤ ), indicating the number of processes.

Then followed N lines, each line contains two integers l_i, r_i (0 ≤ l_ir_i), indicating the temperature range of the i-th process.

输出描述

For each test case, each line contains an integer, indicating the shortest time the kitchen take to grill steak each case.

示例1

输入:

2
3
1 5
4 6
2 3
3
0 6
0 8
0 4

输出:

8
3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 630ms, 内存消耗: 504K, 提交时间: 2020-09-26 16:34:42

#include<bits/stdc++.h>
using namespace std;
int main(){
	int xx,c,d,n,m,i,j;
	long long a,b,x,y;
	cin>>xx;
	for(int l=0;l<xx;l++){
		cin>>n;
		x=0,y=0;
		for(i=0;i<n;i++){
			cin>>a>>b;
			if(x<=a) y=y+a-x+1,x=a;
			else if(x<=b) y=y+1;
			else y=y+x-b+1,x=b;
		}
		cout<<y<<endl;
	}
	return 0;
}

pypy3(pypy3.6.1) 解法, 执行用时: 1695ms, 内存消耗: 29528K, 提交时间: 2020-10-23 17:56:34

t = int(input())
for t in range(0, t):
    n = int(input())
    ans = n
    temp = 0
    for i in range(0, n):
        l, r = map(int, input().split())
        if temp>r:
            ans+=temp-r
            temp = r
        elif temp<l:
            ans+=l-temp
            temp = l
    print(ans)

C++14(g++5.4) 解法, 执行用时: 736ms, 内存消耗: 384K, 提交时间: 2020-09-26 15:53:47

#include<bits/stdc++.h>
using namespace std;

int main(){
	long long t,n;
	cin>>t;
	while(t--){
		cin>>n;
		long long sum=0,c=0,l,r;
		for(long long i=0;i<n;i++){
			cin>>l>>r;
			if(c<l)sum+=(l-c+1),c=l;
			else if(c>r)sum+=(c-r+1),c=r;
			else sum++;
		}
		cout<<sum<<endl;
	}
	return 0;
}

Python3(3.9) 解法, 执行用时: 1938ms, 内存消耗: 2816K, 提交时间: 2021-03-08 14:17:46

for _ in range(int(input())):
    n=int(input())
    t=0
    for i in range(n):
        x,y=map(int,input().split())
        if t>y:
            n=n+t-y
            t=y
        if t<x:
            n=n+x-t
            t=x

    print(n)

上一题