Circle of Monsters

C. Circle of Monsters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are playing another computer game, and now you have to slay nn monsters. These monsters are standing in a circle, numbered clockwise from 11 to nn. Initially, the ii-th monster has aiai health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 11 (deals 11 damage to it). Furthermore, when the health of some monster ii becomes 00 or less than 00, it dies and explodes, dealing bibi damage to the next monster (monster i+1i+1, if i<ni<n, or monster 11, if i=ni=n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all nn monsters in the circle.

Input

The first line contains one integer TT (1T1500001≤T≤150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer nn (2n3000002≤n≤300000) — the number of monsters. Then nn lines follow, each containing two integers aiai and bibi (1ai,bi10121≤ai,bi≤1012) — the parameters of the ii-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000300000.

Output

For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example
input
Copy
1
3
7 15
2 14
5 3
output
Copy
6

 一道有意思的贪心

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll  long long
#define PII  pair<int, int>
using namespace std;
int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int maxn = 2005;
//if(x<0 || x>=r || y<0 || y>=c)
//1000000000000000000

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

bool cmp(pair<int, int>x, pair<int, int>y)
{
    return x.second > y.second;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        n=read();
        vector<ll> a(n+1), b(n+1);
        for (int i = 1; i <= n; i++)
        {
            a[i] = read();
            b[i] = read();
        }
        a[0] = a[n];
        b[0] = b[n];
        ll f=INF, s=0;
        for (int i = 1; i <= n; i++)
        {
            s += max(a[i] - b[i - 1], 0ll);
            f = min(f, a[i]-max(a[i] - b[i - 1], 0ll));
        }
        cout << f + s << endl;
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/dealer/p/12677825.html