Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化
https://codeforces.com/contest/958/problem/E2
首先求出N个时刻的N-1个间隔长度,问题就相当于在这些间隔中选K个数,相邻两个不能同时选,要求和最小
方法1:
一个K^2的做法,有一定技巧
https://www.cnblogs.com/void-f/p/8867585.html
方法2:
是可撤销贪心的模板?
就是贪心的选权值最小的,但是在选完某一个位置i后把它前一个没有被删的位置pre[i]和后一个没有被删的位置nxt[i]删掉,将i的权值变为(-(i原来的权值)+(pre[i]原来的权值)+(nxt[i]原来的权值)),表示如果再选一次这个位置,就相当于不选i,改为选pre[i]和nxt[i]
这样子搞是因为:由于每次是贪心的选权值最小的i,则单独选pre[i]和nxt[i]中任意一个都不可能比选i更优,只有当同时选pre[i]和nxt[i]时可能比选i要优
细节并不能搞清楚...然而,这个算法运行得很正确
有一个要注意的:如果选出的是最靠边上的i,那么显然i一定在最优解中(因为改为选i旁边的一定不会更优),直接把自身和与其相邻的删掉即可
可以搞一个链表+堆维护这个东西
错误记录:链表写错
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
struct E
{
int nxt,pre,d;
}e[];
int a[];
int K,n;
set<pii> s;
set<pii>::iterator i1;
int an;
int main()
{
int i,x,y;pii t;
scanf("%d%d",&K,&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
for(i=;i<n;i++) a[i]=a[i+]-a[i];
n--;
for(i=;i<=n;i++)
{
e[i].nxt=i+;
e[i].pre=i-;
e[i].d=a[i];
s.insert(mp(e[i].d,i));
}
e[n].nxt=e[].pre=;
for(i=;i<=K;i++)
{
i1=s.begin();
t=*i1;s.erase(i1);
x=t.fi;y=t.se;
an+=x;
x=-x;
if(!e[y].pre||!e[y].nxt)
{
if(e[y].pre)
{
s.erase(mp(e[e[y].pre].d,e[y].pre));
e[e[e[y].pre].pre].nxt=;
}
if(e[y].nxt)
{
s.erase(mp(e[e[y].nxt].d,e[y].nxt));
e[e[e[y].nxt].nxt].pre=;
}
}
else
{
x+=e[e[y].pre].d;x+=e[e[y].nxt].d;
s.erase(mp(e[e[y].pre].d,e[y].pre));
s.erase(mp(e[e[y].nxt].d,e[y].nxt));
e[y].pre=e[e[y].pre].pre;e[e[y].pre].nxt=y;
e[y].nxt=e[e[y].nxt].nxt;e[e[y].nxt].pre=y;
e[y].d=x;
s.insert(mp(e[y].d,y));
}
}
printf("%d",an);
return ;
}
方法3:
为什么说是什么带权二分?以后再说
以下题改一下以上代码读入中n和K的顺序就可以A掉了??
https://www.lydsy.com/JudgeOnline/problem.php?id=1150
bzoj 2151 种树
https://www.lydsy.com/JudgeOnline/problem.php?id=2151
洛谷P1792
https://www.luogu.org/problemnew/show/P1792
(另有一样的题(编译优化):http://210.33.19.103/contest/982/problem/5)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int> pli;
struct E
{
int nxt,pre;ll d;
}e[];
int a[];
int n,m;
set<pli> s;
set<pli>::iterator i1;
ll an;
int main()
{
int i,y;ll x;pli t;
scanf("%d%d",&n,&m);
if(m>n/)
{
puts("Error!");
return ;
}
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n;i++)
{
e[i].nxt=i+;
e[i].pre=i-;
e[i].d=a[i];
s.insert(mp(e[i].d,i));
}
e[n].nxt=;e[].pre=n;
for(i=;i<=m;i++)
{
i1=s.end();--i1;
t=*i1;s.erase(i1);
x=t.fi;y=t.se;
an+=x;
x=-x;
x+=e[e[y].pre].d;x+=e[e[y].nxt].d;
s.erase(mp(e[e[y].pre].d,e[y].pre));
s.erase(mp(e[e[y].nxt].d,e[y].nxt));
e[y].pre=e[e[y].pre].pre;e[e[y].pre].nxt=y;
e[y].nxt=e[e[y].nxt].nxt;e[e[y].nxt].pre=y;
e[y].d=x;
s.insert(mp(e[y].d,y));
}
printf("%lld",an);
return ;
}
Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化的更多相关文章
- [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)
[BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...
- 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)
洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...
- bzoj 4816: 洛谷 P3704: [SDOI2017]数字表格
洛谷很早以前就写过了,今天交到bzoj发现TLE了. 检查了一下发现自己复杂度是错的. 题目传送门:洛谷P3704. 题意简述: 求 \(\prod_{i=1}^{N}\prod_{j=1}^{M}F ...
- bzoj 1014: 洛谷 P4036: [JSOI2008]火星人
题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...
- bzoj 3680(洛谷1337) 吊打XXX——模拟退火
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3680 https://www.luogu.org/problemnew/show/P1337 ...
- bzoj 4592(洛谷 4344) [Shoi2015]脑洞治疗仪——线段树上二分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4592 1操作就是用线段树来二分找到第一个有 k 个0的位置. 在洛谷上A了,与暴力和网上题解 ...
- bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演
题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005 洛谷 P1447 https://www.luogu.org/ ...
- BZOJ 1901 洛谷 P2617 ZOJ 2112 Dynamic Rankings
以下时空限制来自zoj Time limit 10000 ms Memory limit 32768 kB OS Linux Source Online Contest of Christopher' ...
- BZOJ 2460 & 洛谷 P4570 [BJWC2011]元素 (线性基 贪心)
题目链接: 洛谷 BZOJ 题意 给定 \(n\) 个矿石,每个矿石有编号和魔力值两种属性,选择一些矿石,使得魔力值最大且编号的异或和不为 0. 思路 线性基 贪心 根据矿石的魔力值从大到小排序. 线 ...
随机推荐
- Linux升级安装GCC G++ 6.2
使用yum安装是不可能了,各大仓库也没有,只能自己编译安装了. 系统为CentOS 6.5,gcc为4.4.7 1 下载源代码包 当前最新版为6.2: wget http://ftp.gnu.org/ ...
- C++中指针和指针变量
指针和指针变量的理解: #include<iostream> using namespace std; int main() { int n; int * m; m = &n; n ...
- BestCoder7 1002 Little Pony and Alohomora Part I(hdu 4986) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4986 题目意思:有 n 个box(从左到右编号依次为1~n),每个box里面有一个随机的钥匙,有可能这 ...
- 2013 gzhu 校赛
题目描述: Integer in C++ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Othe ...
- 第三篇:python基础之数据类型与变量
阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整型long: 2.2.1.3 布尔bool ...
- bleve搜索引擎是支持基于field搜索的
Query String Query The query language query allows humans to describe complex queries using a simple ...
- 51nod-1065:最小正子段和(STL)
N个整数组成的序列a11,a22,a33,…,ann,从中选出一个子序列(aii,ai+1i+1,…ajj),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的. 例如:4,-1 ...
- netty学习2
一.Netty分层设计 Netty 采用了比较典型的三层网络架构进行设计,逻辑架构图如下所示: #第一层,Reactor 通信调度层,它由一系列辅助类完成,包括 Reactor 线程 NioEvent ...
- Strom配置说明
在进群生产环境下运行Topology和在本地模式下运行非常相似.下面是步骤: 1.定义Topology(如果使用Java开发语言,则使用TopologyBuilder来创建) 2.使用StormSub ...
- 【旧文章搬运】为什么win32k.sys在System进程空间无法访问
原文发表于百度空间,2010-01-02========================================================================== 玩过Sha ...