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

题目链接

题意:

给定3个人互不相同的多个数字,可以把数字移动给别人,问最少移动几次后可以使第一个人的数字为1m1,第二个人m1m2,第三个人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)} 查询操作可用线段树维护

代码:

#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;
}

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

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

    E. The Contest A team of three programmers is going to play a contest. The contest consists of

  2. Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)

    题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...

  3. Educational Codeforces Round 76 (Rated for Div. 2)

    传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...

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

    D. Yet Another Monster Killing Problem You play a computer game. In this game, you lead a party of

  5. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题

    C. Dominated Subarray Let's call an array

  6. Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick 水题

    B. Magic Stick Recently Petya walked in the forest and found a magic stick. Since Petya really likes ...

  7. Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题

    A. Two Rival Students There are

  8. Educational Codeforces Round 76 (Rated for Div. 2) D题

    题意: 给你n个关卡,每个关卡有一个怪物,怪物的攻击力为a[i],你有n个英雄,每个英雄有一个攻击力,和疲劳值,只要英雄的攻击力比怪物的高就算打过了,同时疲劳减一,一天只能出战一个英雄,一个英雄可以打 ...

  9. Educational Codeforces Round 76 (Rated for Div. 2) D

    D题 原题链接 题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...

随机推荐

  1. web前端常用知识点

    1.常见的块级元素  内联元素   div -最常用的块级元素      dl - 和dt-dd 搭配使用的块级元素      form - 交互表单      h1 -h6- 大标题      hr ...

  2. License for package Android SDK Build-Tools 28.0.3 not accepted

    License for package Android SDK Build-Tools 28.0.3 not accepted 用flutter进行编写时出现了标题的错误,不是配置的原因,而是需要接受 ...

  3. hadoop配置环境变量

    hadoop安装包解压 tar -xvf hadoop-2.7.7.tar.gz 解压成功ll查看文件 配置环境变量 1. vi  /home/wj/hadoop-2.7.7/etc/hadoop/h ...

  4. 【转】经典的SQL语句面试题

    Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname,T#) 课程表SC(S#,C#,score) 成绩表Teacher(T#,Tname) 教师表 问题: 1 ...

  5. 【转】在NetBeans上搭建Android SDK环境

    本文将介绍在NetBeans 6.8上搭建Android SDK环境,目前Android在Netbeans上进行开发需要借助nbandroid的平台插件. 我们刚刚介绍过<MyEclipse上搭 ...

  6. MySQL 物理备份工具-xtrabackup

    安装 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum -y install perl ...

  7. java 常用锁

    公平锁和非公平锁 1.公平锁,是指多个线程按照申请的顺序来获取锁,类似排队打饭,先来后到. 2.非公平锁,是指多个线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程 比先申请的线程优先获取锁, ...

  8. JAVA中常用的异常处理方法

    1.在Java项目中经常遇到的异常情况 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastExceptio ...

  9. 本地缓存google.guava及分布式缓存redis 随笔

    近期项目用到了缓存,我选用的是主流的google.guava作本地缓存,redis作分布式 缓存,先说说我对本地缓存和分布式缓存的理解吧,可能不太成熟的地方,大家指出,一起 学习.本地缓存的特点是速度 ...

  10. Django redis 应用

    一.自定义连接池 与python中使用连接池一样(使用单例对象) 注意:每个视图函数都要有 conn = redis.Redis(connection_pool=POOL) 二.使用第三方模块(dja ...