[LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心
伊斯坦布尔的帮派Gangs of Istanbull
题目链接:https://www.luogu.org/problem/P3064
数据范围:略。
题解:
这个题其实分为两问,第一问是$YES$、$NO$和最大值,第二问是最小字典序方案。
整体思路肯定是,后$2\sim m$的帮派先自行抵消,最少能剩下多少奶牛,然后再用$1$去抵消。
先说第一问:
问题就相当于求$k$堆奶牛最少抵消成多少头。
这个最傻逼的做法就是维护一个大根堆,把$2\sim m$都扔进去。
然后每次取出人数最多的两个帮派,让它俩互相抵消一次,再扔回堆里,这是$O(nlogn)$的。
再来看第二问:
我们发现,如果按照第一问的思路,第二问根本就没法做。
因为第一问的过程我们根本就没办法掌控,但是它给了我们一些启发。
再画几组小数据我们发现,最少剩多少奶牛其实只和这$2\sim m$中的最大值有关。
这是显然的,那么我们假设这些奶牛的和为$sum$,最大值为$mx$,分两种情况讨论:
第一种:$mx > \frac{sum}{2}$。
这种就比较简单,因为所有的非最大值奶牛一定是要和最大值相抵消的。
那么我们把答案大小的$1$号放在最后,剩下的随便搞搞基就好,具体看代码。
第二种:$mx \le \frac{sum}{2}$。
这种的话,最少会剩下$sum \& 1$头,假设是$0$。
那么所有的$1$奶牛都得扔在后面,我们只需要考虑剩下的帮派怎么互相抵消就好。
考虑每次贪心。
假设现在已经决定了前$i - 1$头奶牛的顺序,剩下$now$头属于帮派$id$的奶牛,我们考虑$i$位置。
首先,取出来最小的有奶牛的帮派和最大的帮派两个。
如果最小的帮派放在了位置$i$,与$now$和$id$做了做抵消之后,剩下的奶牛仍然满足$Max \le \frac{Sum}{2}$,我们就放最小的。
显然如果最小的不行,不是最大值不是最小值的其他任何数都不行(除了$id$,可以做一下特判)。
如果都不行,那么这个位置只能是最大值,我们就把最大值所在的帮派的奶牛数$--$,然后考虑位置$i + 1$即可。
这时候,我们想一想怎么能拿出来最大值最小值呢?还要支持单点修改?那就维护一个线段树好了。
代码:
#include <bits/stdc++.h> #define N 1000010 #define ls p << 1 #define rs p << 1 | 1 using namespace std; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int a[N]; int sum[N << 2], mx[N << 2]; inline void pushup(int p) {
mx[p] = max(mx[ls], mx[rs]);
sum[p] = sum[ls] + sum[rs];
} void update(int x, int v, int l, int r, int p) {
if (l == r) {
sum[p] += v, mx[p] += v;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) {
update(x, v, l, mid, ls);
}
else {
update(x, v, mid + 1, r, rs);
}
pushup(p);
} int query_id(int l, int r, int p) {
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
if (sum[ls]) {
return query_id(l, mid, ls);
}
else {
return query_id(mid + 1, r, rs);
}
} int query_mx(int l, int r, int p) {
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
if (mx[ls] >= mx[rs]) {
return query_mx(l, mid, ls);
}
else {
return query_mx(mid + 1, r, rs);
}
} int main() {
// freopen("gangs.in", "r", stdin);
// freopen("gangs.out", "w", stdout);
int n = rd(), m = rd();
for (int i = 1; i <= m; i ++ ) {
a[i] = rd();
}
int Sum = n - a[1], mx = 0;
for (int i = 2; i <= m; i ++ ) {
mx = max(mx, a[i]);
}
int re;
int flag = 0;
if (mx <= Sum / 2) {
flag = 1;
re = Sum & 1;
if (re) {
flag = 2;
}
}
else {
int mdl = Sum - mx;
re = mx - mdl;
flag = 3;
}
if (re >= a[1]) {
puts("NO");
return 0;
} puts("YES");
printf("%d\n", a[1] - re);
if (flag == 1) {
// puts("Fuck");
for (int i = 2; i <= m; i ++ ) {
update(i, a[i], 1, m, 1);
}
int id = 0, now = 0;
for (int i = 1; i <= n - a[1]; i ++ ) {
int j = query_id(1, m, 1);
if (!id) {
printf("%d\n", j);
id = j, now = 1;
a[j] -- ;
update(j, -1, 1, m, 1);
continue;
}
if (id == j) {
printf("%d\n", j);
now ++ ;
a[j] -- ;
update(j, -1, 1, m, 1);
continue;
}
int mdlall = now + sum[1] - 2;
// puts("SSSShit");
int mdlmxid = query_mx(1, m, 1);
// cout << mdlall << ' ' << mdlmxid << endl ;
int mdlmx = a[mdlmxid];
if (mdlmxid == j) {
mdlmx -- ;
}
if (mdlmx <= mdlall / 2) {
printf("%d\n", j);
update(j, -1, 1, m, 1);
a[j] -- ;
now -- ;
if (!now) {
id = 0;
}
}
else {
printf("%d\n", mdlmxid);
update(mdlmxid, -1, 1, m, 1);
a[mdlmxid] -- ;
now -- ;
if (!now) {
id = 0;
}
}
}
for (int i = 1; i <= a[1]; i ++ ) {
printf("%d\n", 1);
}
}
else if (flag == 2) {
// puts("Fuck");
update(1, 1, 1, m, 1);
for (int i = 2; i <= m; i ++ ) {
update(i, a[i], 1, m, 1);
}
int id = 0, now = 0;
int all = n - a[1] + 1;
for (int i = 1; i <= all; i ++ ) {
int j = query_id(1, m, 1);
if (!id) {
printf("%d\n", j);
id = j, now = 1;
a[j] -- ;
update(j, -1, 1, m, 1);
continue;
}
if (id == j) {
printf("%d\n", j);
now ++ ;
a[j] -- ;
update(j, -1, 1, m, 1);
continue;
}
int mdlall = now + sum[1] - 2;
int mdlmxid = query_mx(1, m, 1);
int mdlmx = a[mdlmxid];
if (mdlmxid == j) {
mdlmx -- ;
}
if (mdlmx <= mdlall / 2) {
printf("%d\n", j);
update(j, -1, 1, m, 1);
a[j] -- ;
now -- ;
if (!now) {
id = 0;
}
}
else {
printf("%d\n", mdlmxid);
update(mdlmxid, -1, 1, m, 1);
a[mdlmxid] -- ;
now -- ;
if (!now) {
id = 0;
}
}
}
// puts("Shit");
// printf("%d\n", a[1]);
for (int i = 1; i <= a[1]; i ++ ) {
printf("%d\n", 1);
}
}
else {
int id = 2;
for (int i = 2; i <= m; i ++ ) {
if (a[i] > a[id]) {
id = i;
}
}
for (int i = 1; i <= re; i ++ ) {
puts("1");
}
for (int i = 1; i <= re; i ++ ) {
printf("%d\n", id);
}
for (int i = 2; i < id; i ++ ) {
for (int j = 1; j <= a[i]; j ++ ) {
printf("%d\n", i);
}
for (int j = 1; j <= a[i]; j ++ ) {
printf("%d\n", id);
}
}
int mdlsum = 0;
for (int i = id + 1; i <= m; i ++ ) {
mdlsum += a[i];
}
for (int i = 1; i <= mdlsum; i ++ ) {
printf("%d\n", id);
}
for (int i = id + 1; i <= m; i ++ ) {
for (int j = 1; j <= a[i]; j ++ ) {
printf("%d\n", i);
}
}
for (int i = 1; i <= a[1] - re; i ++ ) {
puts("1");
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
小结:好题好题,但是细节有点点多。考试的时候没拿到$flag=2$的点,原因是$for$循环的问题。所以如果一个题有多种情况,最好每种情况都试几组小样例。
[LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心的更多相关文章
- P3064 [USACO12DEC]伊斯坦布尔的帮派 (模拟)
题目传送门 题意: 一片草地,每次可以只可以让一种牛占领,问你怎样安排牛的次序 最后剩下的是1号牛,并且输出其数量 思路: 看到n到100 ,所以可以(n^3)暴力,第一重遍历次序,第二枚举是哪只牛 ...
- 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)
题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- ACM起步要点总结(转哈工大)
首先,我想说的就是,我是一个很普通的ACMer,高中没有参加过任何计算机和数学竞赛的经历,也没有ben那样过人的天资,努力至今也未能取得什么成绩,我之所以写下这篇文章,只是希望给刚进大学或者刚进ACM ...
- NOIP2017 国庆郑州集训知识梳理汇总
第一天 基础算法&&数学 day1难度测试 如果要用一个词来形容上午的测试,那真是体无完肤. 成绩: 题目 成绩 评价 T1 50 一般 T2 10 大失所望 T3 0 差 基础算法 ...
- [Swift]LeetCode879. 盈利计划 | Profitable Schemes
There are G people in a gang, and a list of various crimes they could commit. The i-th crime generat ...
- my.资料收集_20170912
1.终于摸索出平民单开赚钱方法了!![梦幻西游手游吧]_百度贴吧.html http://tieba.baidu.com/p/5323468885?see_lz=1 1.http://tieba.ba ...
- Description POJ1703
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
随机推荐
- 【线性代数】5-3:克莱姆法则,逆和体积(Cramer's Rule,Inverses,and Volumes)
title: [线性代数]5-3:克莱姆法则,逆和体积(Cramer's Rule,Inverses,and Volumes) categories: Mathematic Linear Algebr ...
- Luogu5369 [PKUSC2018]最大前缀和
题目链接:洛谷 题目大意:给定一个长为$n$的整数序列,求全排列的最大前缀和(必须包含第一个数)之和. 数据范围:$1\leq n\leq 20,1\leq \sum_{i=1}^n|a_i|\leq ...
- SpringAOP配置与使用(示例)
1.pom.xml追加 spring-aspects aspectjrt 为控制器以外的类织入切面 2.新建spring-aop.xml <?xml version="1.0" ...
- Exception in thread "main" java.util.ConcurrentModificationException解决方案
我想判断一个集合里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素, 当时的做法是: public class ListIterator ...
- 表单 Flask-WTF - 使用
1 配置 可以使用Flask-WTF来处理web表单,在使用之前要先配置下,打开config.py,编辑添加如下内容 WTF_CSRF_ENABLED = True SECRET_KEY = 'you ...
- 设置Python打印格式
>>> def esc(code): ... return f'\033[{code}m' ... >>> print(esc('31;1;4') + 'reall ...
- arcpy 获得是否为布局mxd.activeView
arcpy 获得是否为布局mxd.activeView print mxd.activeView PAGE_LAYOUT mxd.pageSizePageSize(width=21.590043180 ...
- 转贴:PLSQL中 commit 和 rollback 的区别
PLSQL中 commit 和 rollback 的区别 原文链接:https://blog.csdn.net/jerrytomcat/article/details/82250915 一. comm ...
- vue单页面项目架构方案
这里的架构方案是基于vue-cli2生成的项目应用程序产生的,是对项目应用程序或者项目模板的一些方便开发和维护的封装.针对单页面的解决方案. 主要有四个方面: 一,不同环境下的分别打包 主要是测试环境 ...
- Eclipse的下载地址
下载地址:http://eclipse.org/