题:https://codeforces.com/contest/1257/problem/E

题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序。

   输出最小操作次数

dp:

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+;
int dp[M][];
int a[M];
///dp[i][0]表示前i个数全属于第一个数组所要花费的最小代价
///dp[i][1]表示前i个数全属于第一、二个数组所要花费的最小代价
///!!dp[i][1]必须要保证前i个数是要连续的属于第一个数组,和连续的属于第一个数组前后分布
int main(){
int k1,k2,k3;
scanf("%d%d%d",&k1,&k2,&k3);
int n=k1+k2+k3;
for(int i=,x;i<=k1;i++){
cin>>x;
a[x]=;
}
for(int i=,x;i<=k2;i++){
cin>>x;
a[x]=;
}
for(int i=,x;i<=k3;i++){
cin>>x;
a[x]=;
}
for(int i=;i<=n;i++){
dp[i][]=dp[i-][]+(a[i]==?:);
dp[i][]=min(dp[i-][],dp[i-][])+(a[i]==?:);
dp[i][]=min(dp[i-][],min(dp[i-][],dp[i-][]))+(a[i]==?:);
}
cout<<min(dp[n][],min(dp[n][],dp[n][]));
return ;
}

贪心:

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+;
int a[M],b[M];
int main(){
int n;
int k1,k2,k3;
scanf("%d%d%d",&k1,&k2,&k3);
n=k1+k2+k3;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+k1);
sort(a+k1,a+k1+k2);
sort(a+k1+k2,a+n);
int tot=;
for(int i=;i<n;i++){
if(b[tot]<a[i])
b[++tot]=a[i];
else{
int pos=upper_bound(b,b+tot,a[i])-b;
b[pos]=a[i];
} }
printf("%d\n",n-tot);return ;
}

题解:枚举R,R往后的给第三个选手,然后在R的左边部分确定一个L,使其价值最优,

   考虑对于确定的L,R,答案就是,不属于L,R分割出来的数组对应的位置。

   比如L前面的数组我们要挑出属于第二,三数组的数

   这个说的就是上面的东西,也就是当前L,R的答案cnt(L,2)+cnt(L,3)+cnt(m,1)+cnt(m,3)+cnt(r,1)+cnt(r,2)

   分析:第二、四、五、六项我们可以通过枚举的R的位置来求到

      记x为第一项和第三项的和,即x=cnt(L,2)+cnt(m,1);

      现在只要知道x我们就可以算这个位置的答案了

      记posval=cnt(L,1)-cnt(L,2),我们要操作数最小,肯定要posval最大,因为posval大说明在L前面有越多满足属于第一个选手的题目。

      这时我们发现,posval+x=cnt(L,1)+cnt(m,1),这个表示R前面1的个数,这个可以很轻易的求得;

      x=cnt(R,1)-posval

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+;
int a[M];
int L[],R[];
int main(){
int k1,k2,k3;
scanf("%d%d%d",&k1,&k2,&k3);
int n=k1+k2+k3;
for(int x,i=;i<=k1;i++){
scanf("%d",&x);
a[x]=;
}
for(int x,i=;i<=k2;i++){
scanf("%d",&x);
a[x]=;
}
for(int x,i=;i<=k3;i++){
scanf("%d",&x);
a[x]=;
}
int ans=,posval=;
for(int i=;i<=n;i++)
if(a[i]!=)
ans++;
for(int i=;i<=n;i++)
R[a[i]]++;
for(int i=;i<=n;i++){
L[a[i]]++;
R[a[i]]--;
posval=max(posval,L[]-L[]);///最佳的位置
int sum=R[]+R[]+L[]+L[]-posval;
ans=min(ans,sum);
}
cout<<ans<<endl;
return ;
}

Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)的更多相关文章

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

    Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...

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

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

  3. 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

  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. Arduino --structure

    The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...

  2. ansible简单部署前端

    pipeline{ agent any parameters { choice(name: 'server_name', choices: ['xx','xx'], description: 'ser ...

  3. java课程课后作业190606之计算最长英语单词链

    一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次.最长的定义是:最多单词数量,和单词中字母的数量无关. 统一输入文件名称:inp ...

  4. Social GAN代码要点记录

    近日在阅读Social GAN文献的实验代码,加深对模型的理解,发现源代码的工程化很强,也比较适合构建实验模型的学习,故细致阅读.下文是笔者阅读中一些要点总结,有关于pytorch,也有关于模型自身的 ...

  5. Spring注解——@Transactional

    @Transactional    用于service实现类,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务.(未完待续)

  6. textare限制拖动;提示文字(点击消失,不输入恢复提示信息)

    1.在textarea添加一个样式:style="resize:none;" 2.提示文字(鼠标点击的时候消失,不输入恢复提示信息): <input name="t ...

  7. shell字典使用

    1.shell界面或脚本中 pcode=(["10"]="BeiJing" ["22"]="TianJin") echo ...

  8. BZOJ [Cqoi2017] 小Q的棋盘

    题解:枚举最后在哪里停止,然后剩下的步数/2 也就是找最大深度 枚举终止位置算是一种思路吧 #include<iostream> #include<cstdio> #inclu ...

  9. 干货|CVE-2019-11043: PHP-FPM在Nginx特定配置下任意代码执行漏洞分析

    近期,国外安全研究员Andrew Danau,在参加夺旗赛(CTF: Capture the Flag)期间,偶然发现php-fpm组件处理特定请求时存在缺陷:在特定Nginx配置下,特定构造的请求会 ...

  10. Linux CMD 笔记 & 正则表达式

    一.linux bash 1. 进程名查找kill ps -ef | grep xxxx| grep -v grep| cut -c 9-15 | xargs kill -9 2.端口号kill 占用 ...