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个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
随机推荐
- 【机器学习实战学习笔记(1-1)】k-近邻算法原理及python实现
笔者本人是个初入机器学习的小白,主要是想把学习过程中的大概知识和自己的一些经验写下来跟大家分享,也可以加强自己的记忆,有不足的地方还望小伙伴们批评指正,点赞评论走起来~ 文章目录 1.k-近邻算法概述 ...
- AttributeError: 'list' object has no attribute 'data'智障错误
import urllib.requestimport urllib.parse url = ['http://fanyi.youdao.com/translate?smartresult=dict& ...
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
- Tensorflow学习教程------简单练一波,线性模型
#coding:utf-8 import tensorflow as tf import numpy as np #使用numpy 生成100个随机点 x_data = np.random.rand( ...
- web页面内容打印总结
web页面打印有两种,一种是直接调用window.print()命令操作,一种是使用ActiveX插件(Object标签)操作,但是第二种只支持IE内核的浏览器. 示例1: <!DOCTYPE ...
- CentOS 7.3 下部署基于 Node.js的微信小程序商城
本文档为微信小程序商城NideShop项目的安装部署教程,欢迎star NideShop商城api服务:https://github.com/tumobi/nideshop NideShop微信小程序 ...
- .NET技术-3.0. 日志插件 log4net
.NET技术-3.0. 日志插件 log4net 背景:框架 NetFramework3.5 1. 新建控制台程序 2. 程序包管理器控制台中 增加 Nuget包 Install-Package lo ...
- Unix网络编程学习 < 一 >
#include "unp.h" int main(int argc , char**argv) { int sockfd , n; //sockfd套接字描述符 ]; struc ...
- 201771010123汪慧和《面向对象程序设计Java》第十周实验总结
一.理论部分 1.泛型:也称参数化类型.就是定义类.接口和方法时,通过类型参数指示将要处理的对象类型. 2.泛型程序设计:编写代码可以被很多不同类型的对象所重用. 3.泛型方法: a.除了泛型类外,还 ...
- css 元素选择器
子元素选择器 h1 > strong {color:red;} //这个规则会把第一个 h1 下面的两个 strong 元素变为红色,但是第二个 h1 中的 strong 不受影响: <h ...