NC212615. CookSteak
描述
输入描述
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 (0 ≤ ≤ ≤ ), 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)