第十七届中国计量大学程序设计竞赛(同步赛)Cook Steak

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 [li,ri][l_i,r_i][li​,ri​] 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 [li,ri][l_i,r_i][li​,ri​], 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 ≤ 10510^5105), indicating the number of processes.

Then followed N lines, each line contains two integers li,ril_i, r_ili​,ri​ (0 ≤ lil_ili​ ≤ rir_iri​ ≤ 10910^9109), 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

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

输出

复制8 3

8
3
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int i,j,k;
        long long int l1,r1,l2,r2,x;

        scanf("%lld %lld",&l1,&r1);
        long long int sum=0,num=0;
        for(i=1; i<n; i++)
        {
            scanf("%lld %lld",&l2,&r2);
            if(l1<=l2&&l2<=r1&&r1<=r2)
            {
                l1=l2;
            }
            else if(l2<=l1&&l1<=r2&&r2<=r1)
            {
                r1=r2;
            }
            else if(l1<=l2&&r1>=r2)
            {
                l1=l2;
                r1=r2;
            }
            else if(l1>=l2&&r1<=r2)
            {
                l1=l1;
                r1=r1;
            }
            else if(r2<l1)
            {
                if(sum==0)
                {
                    sum=sum+l1;
                    x=l1;
                }
                sum=sum+l1-r2;
                sum=sum+abs(l1-x);
                x=r2;
                l1=l2;
                r1=r2;
            }
            else if(l2>r1)
            {
                if(sum==0)
                {
                    sum=sum+r1;
                    x=r1;
                }
                sum=sum+l2-r1;
                sum=sum+abs(r1-x);
                x=l2;
                l1=l2;
                r1=r2;
            }
        }
        if(sum==0)
        {
           sum=sum+l1;
        }
        if(x>r1)
        {
           sum=sum+x-r1;
        }
        if(x<l1)
        {
           sum=sum+l1-x;
        }
        sum=sum+n;
        printf("%lld\n",sum);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值