Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题: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||贪心||题解写法)的更多相关文章
- Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest
Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定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 ...
- 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
- 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
- Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题
C. Dominated Subarray Let's call an array
- 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 ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题
A. Two Rival Students There are
- Educational Codeforces Round 76 (Rated for Div. 2) D题
题意: 给你n个关卡,每个关卡有一个怪物,怪物的攻击力为a[i],你有n个英雄,每个英雄有一个攻击力,和疲劳值,只要英雄的攻击力比怪物的高就算打过了,同时疲劳减一,一天只能出战一个英雄,一个英雄可以打 ...
- Educational Codeforces Round 76 (Rated for Div. 2) D
D题 原题链接 题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
随机推荐
- 自定义css
/** * 重写FrozenUI */ /** * 按钮 */ body { background-color: #F2F2F2; } @media screen and (-webkit-min-d ...
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- cf 758D - Ability To Convert
从后往前贪心就好了.各种各样0的情况太BT了.. (各种爆long long,f**k) #include<bits/stdc++.h> #define LL long long #def ...
- HTTP协议、时间戳
1.什么是HTTP协议 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的 ...
- jQuery元素的左右移动
1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...
- bugku-Web 文件包含2
打开网页查看源代码,在最上面发现upload.php,于是我们就访问这个文件:http://123.206.31.85:49166/index.php?file=upload.php 发现是让我们上传 ...
- SDN(Software Defined Network):软件定义网络----转载
SDN(Software Defined Network):软件定义网络 传统的网络转发行为: 1)逐设备单独控制,纯分布式控制. 2)控制面和转发面在同一个设备中,耦合紧密. 管理员无法直接操控转发 ...
- 201771010123汪慧和《面向对象程序设计Java》第十四周实验总结
一.理论部分 1.Swing和MVC设计模式 (1)设计模式初识 (2)模式—试图—控制器模式 (3)Swing组件的模型—试图—控制器分析 2.Java组件有内容.外观.行为三个主要元素:这三个主要 ...
- Angular表单 (一)表单简介
Angular 表单 angular提供了两种不同的方法来通过表单处理用户输入:响应式表单和模板驱动表单.二者都从视图中捕获用户输入事件.验证用户输入.创建表单模型.修改数据模型,并提供跟踪这些更改的 ...
- pytorch 自动求梯度
自动求梯度 在深度学习中,我们经常需要对函数求梯度(gradient).PyTorch提供的autograd包能够根据输入和前向传播过程自动构建计算图,并执行反向传播.本节将介绍如何使用autogra ...