Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest

Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树)

题目链接

题意:

给定3个人互不相同的多个数字,可以把数字移动给别人,问最少移动几次后可以使第一个人的数字为1~m1,第二个人m1~m2,第三个人m2~n(可以没有数字)

题解:

设c[1][i]为第一个人m1为i时需要移动的次数,c[3][i]为m2为i是第三个人需要操作的次数,当其他两个人数字合法时,第二个人的数字也会合法.枚举第一个人的每个i,查询m2为(i+1~n+1)的最小操作次数,ans = min{c[1][i]+min(c[3][k](i<k<=n))} 查询操作可用线段树维护

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 250000;
int t[4*N],a[5][N],b[5][N],c[5][N];
void build(int x,int l,int r)
{
    if (l == r)
    {
        t[x] = c[3][l];
        return;
    }
    int mid = (l + r) >>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    t[x] = min(t[x<<1] , t[x<<1|1]);
}
int query(int x,int l,int r,int ll,int rr)
{
    if (ll <= l && r <= rr) return t[x];
    int mid = (l + r) >> 1;
    int ans = 1<<30;
    if (ll <= mid) ans = min(ans,query(x<<1,l,mid,ll,rr));
    if (rr > mid ) ans = min(ans,query(x<<1|1,mid+1,r,ll,rr));
    return ans;
}
int main()
{
    int k1,k2,k3;
    cin >> k1 >> k2 >> k3;
    int n = k1+k2+k3;
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for (int i = 1; i <= k1; i++)
    {
        cin >> a[1][i];
        b[1][a[1][i]] = 1;
    }
    for (int i = 1; i <= k2; i++) cin >> a[2][i];
    for (int i = 1; i <= k3; i++)
    {
        cin >> a[3][i];
        b[3][a[3][i]] = 1;
    }
    c[1][0] = k1;
    for (int i = 1; i <= n; i++)
    {
        b[1][i] = b[1][i-1]+b[1][i]; //记录第一个人前i个数中拥有几个
        c[1][i] = i - b[1][i] + k1 - b[1][i];//i-b[1][i]为需要移动到第一个人的操作数,k1-b[1][i]为第一个人把数移动出去的操作数
    }
    c[3][n+1] = k3;
    for (int i = n; i; i--)
    {
        b[3][i] = b[3][i+1] + b[3][i];
        c[3][i] = n-i+1-b[3][i] +k3 - b[3][i]-(k1-b[1][i-1]); //如果是第一个人可以移动到第三个人的数会重复需要减掉
    }
    for (int i = 1; i < n; i++)
     c[1][i] -= k3-b[3][i+1];//第三个人可以移动到第一个人的数
    build(1,0,n+1);
    int ans = c[1][n];
    for (int i = 0; i < n; i++)
    {
        ans = min(ans, c[1][i] + query(1,0,n,i+1,n+1));
    }
    cout << ans << endl;
}

原文地址:https://www.cnblogs.com/7osen/p/11855716.html

时间: 2024-07-30 07:48:23

Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest的相关文章

Educational Codeforces Round 76 (Rated for Div. 2)

A - Two Rival Students 题意:共n个人排一排,两个人,位于a,b,相邻位置交换至多x次,最大化abs(a-b)的值. 题解:每次交换至多+1,不能超过n-1. #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { #ifdef KisekiPurin freopen("KisekiPurin.in", "r", stdin);

Educational Codeforces Round 76 (Rated for Div. 2) - D. Yet Another Monster Killing Problem(贪心)

题意:有$n$个怪物,每个怪物有一个能力值$a[i]$,你现在有$m$个英雄,每个英雄有两个属性:$p[i]$表示这个英雄的能力值,$s[i]$表示这个英雄的耐力值,即一天内最多能消灭$s[i]$个怪物,每一天你可以选择一个英雄去消灭怪物,并且你只能一个一个的消灭,不能改变顺序,当一个英雄的能力值大于等于怪物的能力值并且他这一天内消灭的怪物数小于$s[i]$时,他就会继续去消灭下一个怪物,直到消灭的怪物数量等于$s[i]$或者这个英雄的能力值小于当前怪物的能力值,结束这一天,进入第二天,一个英雄

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html