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个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
随机推荐
- 15 ~ express ~ 用户数据分页原理和实现
一,在后台路由 /router/admin.js 中 1,限制获取的数据条数 : User.find().limit(Number) 2,忽略数据的前(Number)条数据 : skip(Number ...
- 自定义checkbox,redio等
直接上代码: 看的懂看,看不懂拉到. .messageState li {list-style: none;float: left;padding-right:30px;font-size: 16px ...
- Standard Aras Dialogs
In a another blog post, we covered how to open dialogs within Aras Innovator using custom forms and ...
- linux服务重启命令
/etc/init.d/sshd restart/etc/init.d/sshd reload systemctl status sshd.servicesystemctl restart sshd. ...
- Windows桌面图标不见了,可能是结束了explorer.exe进程导致
Windows桌面图标不见了,怎么办?那么可能是你关掉了explorer.exe的进程. 解决办法: ① Ctrl+shift+delete打开任务管理器,查看进程是否有explorer.exe ② ...
- 关于indexOf的用法
var fullTaskName = this.form.taskName; var index=fullTaskName.lastIndexOf("-"); ...
- (2)MongoDB副本集自动故障转移全流程原理
前文我们搭建MongoDB三成员副本集,了解集群基本特性,今天我们围绕下图聊一聊背后的细节. 默认搭建的replica set均在主节点读写,辅助节点冗余部署,形成高可用和备份, 具备自动故障转移的能 ...
- 线段树--线段树【模板1】P3372
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入格式 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含 ...
- PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- A - Alice and the List of Presents (排列组合+快速幂取模)
https://codeforces.com/contest/1236/problem/B Alice got many presents these days. So she decided to ...