cf暑假训练 1700-1800 day2

cf暑假训练 1700-1800 day2

    • 1779D Boris and His Amazing Haircut(线段树)
    • 1776F Train Splitting

1779D Boris and His Amazing Haircut(线段树)

本题用线段树或者树状数组或者栈都行,只要能获[l,r]上的最大值即可
看的这篇题解

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
struct Node{
    int l, r;
    int v, vmax;
}tr[N * 4];
int a[N], b[N];
void pushup(Node &u, Node &left, Node &right)
{
    u.vmax = max(left.vmax, right.vmax);
}
void pushup(int u)
{
    pushup(tr[u], tr[u << 1], tr[u << 1|1]);
}

void build(int u, int l, int r)
{
    if (l == r) tr[u] = {l, r, b[l], b[l]};
    else 
    {
        tr[u] = {l, r};
        int mid = l + r >> 1;
        build(u << 1, l, mid); build(u << 1|1, mid + 1, r);
        pushup(u);
    }
}
int get(int u, int l, int r)
{
    int res = 0;
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].vmax;
    else//query的另一个模版
    {   
         int mid = tr[u].l + tr[u].r >> 1;
        // if (mid >= l)
        //     res = max(res, get(u << 1, l, r)); // 访问左区间
        // if (mid < r)
        //     res = max(res, get(u << 1 | 1, l, r)); // 访问右区间
        // pushup(u);
        if (l > mid) return get(u << 1|1, l, r);
        if (r <= mid) return get(u << 1, l , r);
        else 
        {
            int left = get(u << 1, l, r);
            int right = get(u << 1|1, l, r);
            return max(left, right);
        }
    }
    return res;
}
void solve()
{  
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++ i)
        cin >> a[i];
    
    map<int, vector<int>> dif;
    int flag = 0;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> b[i];
        if (b[i] > a[i]) flag = 1;
        if (b[i] != a[i]) dif[b[i]].push_back(i);
    }
    build(1, 1, n);
    int m;
    cin >> m;
    vector<int> r;
    map<int, int> cnt_r;
    for (int i = 1; i <= m; ++ i)
    {
        int x;
        cin >> x;
        if (cnt_r[x] == 0) r.push_back(x);
        cnt_r[x] ++;
    }

    for (int i = 1; i <= n; ++ i)
    {
        if (a[i] != b[i] && cnt_r[b[i]]  == 0) flag = 1;
    }
    if (flag)
    {
        cout << "NO" << endl;
        return;
    }

    for (int i = 0; i < r.size(); ++ i)
    {
        int rmax = cnt_r[r[i]];
        int cur = 1;
        int kmax = dif[r[i]].size();
        if (kmax == 0) continue;
        for (int k = 0; k < kmax - 1; ++ k)
        {
            if (get(1, dif[r[i]][k], dif[r[i]][k + 1]) > r[i]) cur++;
        }
        if (cur > rmax) flag = 1;
    }

    if (flag) cout << "NO" << endl;
    else cout << "YES" << endl;
}
int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

1776F Train Splitting

trick:

  • int u = min_element(deg + 1, deg+1+n) - deg;获得一个数组里面最小元素的下标
  • *min_element(deg + 1, deg + 1 + n)获得一个数组里面的最小元素的值
  • 看题解的时候记得多画图,不然干瞪眼看半天也没办法理解
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef pair<int, int> pii;
const int N = 51;
int deg[N];
void solve()
{
    vector<int> v_e[N];
    int n, m;
    cin >> n >> m;
    
    for (int i = 1; i <= m; ++ i)
    {
        int u, v;
        cin >> u >> v;
        deg[u] ++; deg[v] ++;
        v_e[u].push_back(i), v_e[v].push_back(i);
    }
    vector<int> ans(m + 1);
    if (*min_element(deg + 1, deg + 1 + n) <= n - 2)
    {
        cout << "2" << endl;
        int u = min_element(deg + 1, deg+1+n) - deg;
        // cout << "u : "<< u << endl;
        for (int i = 0; i < deg[u]; ++ i) ans[v_e[u][i]] = 1;
        for (int i = 1; i <= m; ++ i) if (ans[i] == 0) ans[i] = 2;
    }
    else 
    {
        cout << "3" << endl;
        ans[v_e[1][0]] = 1;
        for (int i = 1; i < deg[1]; ++ i) ans[v_e[1][i]] = 2;
        for (int i = 1; i <= m; ++ i) if (ans[i] == 0) ans[i] = 3;
    }

    for (int i = 1; i <= m; ++ i) cout << ans[i] << " ";
    cout << endl;
    for (int i = 1; i <= n; ++ i) deg[i] = 0;
}
int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

你可能感兴趣的:(算法)