Educational Codeforces Round 121 (Rated for Div. 2) C解题报告

Posted skywalker767

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 121 (Rated for Div. 2) C解题报告相关的知识,希望对你有一定的参考价值。

比赛降智,还好url(偷笑




**题目思路:**区间合并一下就ok了

#include <bits/stdc++.h>
#define int long long
#define forn(i , n) for(int i = 0;i < n; i ++)
using namespace std;

int T;


bool cmp(pair<int , int> a , pair<int , int> b)

    if (a.first == b.first)
    
        return a.second < b.second;
    
    return a.first < b.first;


vector<pair<int , int> > mer(vector<pair<int , int> > ans)

    vector<pair<int , int> > res;
    sort(ans.begin() , ans.end() , cmp);
    int l = ans[0].first;
    int r = ans[0].second;
    int n = ans.size();
    for (int i = 1;i < n;i ++)
    
        int nowl = ans[i].first;
        int nowr = ans[i].second;
        if (l <= nowl && nowr <= r) continue;
        else if(l <= nowl && nowl <= r && nowr > r) 
            r = nowr;
        
        else 
            res.push_back(make_pair(l , r));
            l = nowl , r = nowr;
        
    
    int t = res.size();
    if (t == 0) res.push_back(make_pair(l , r));
    else if (res[t - 1].first != l && res[t - 1].second != r)res.push_back(make_pair(l , r));
    return res;


signed main()

    cin >> T;
    while (T --)
    
        unordered_map<int , int> ump;
        int n;cin >> n;
        vector<int> h(n) , k(n);
        forn(i , n) 
            cin >> k[i];
            ump[k[i]] = i;
        
        forn(i , n) cin >> h[i];
        vector<pair<int , int> > ans(n);
        forn(i , n) 
            int l = k[i] - h[i] + 1;
            int r = k[i];
            // cout << l << "->" << r << endl;
            ans[i] = make_pair(l , r);
        
        ans = mer(ans);
        n = ans.size();
        int res = 0;
        for (int i = 0;i < n;i ++)
        
            // cout << ans[i].first << " " << ans[i].second << endl;
            int len = ans[i].second - ans[i].first  + 1;
            res += (1 + len) * len / 2;
        
        cout << res << endl;
    
    return 0;

以上是关于Educational Codeforces Round 121 (Rated for Div. 2) C解题报告的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27