有生之年我居然也能不看题解做出来题QAQ……

发现c、d是0、1序列而不是随机数列说明有蹊跷,于是发现负数直接配0,正数配1即可。不知道哪个最小,那就全求一下吧……我的做法的坑点是数正好为1时不可以选。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
const int maxn = 1e5 + 5;
const ll inf = 1e18; int n, a[maxn], b[maxn], ansc[maxn], ansd[maxn];
ll ans = -inf; void solve(int *a, int *b, int *ansc, int *ansd) {
int c[maxn], d[maxn], t[maxn]; ll sum = 0, C = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > 1) {
c[i] = 1;
C++;
sum += a[i];
} else {
c[i] = 0;
}
}
ll k = 0, D = 0;
for (int i = 1; i <= n; i++) {
t[i] = i;
d[i] = 0;
}
sort(t + 1, t + 1 + n, [&](int x, int y) { return b[x] > b[y]; });
for (int i = 1; i <= n; i++) {
int tmp = t[i];
if (b[tmp] <= 1) break;
d[tmp] = 1;
D++;
k += b[tmp];
if (k >= sum) break;
}
if (k >= sum) {
ll calc = sum - C - D;
if (calc > ans) {
ans = calc;
memcpy(ansc, c, sizeof c);
memcpy(ansd, d, sizeof d);
}
}
} int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]); solve(a, b, ansc, ansd);
solve(b, a, ansd, ansc); printf("%lld\n", ans);
for (int i = 1; i <= n; i++)
printf("%d%c", ansc[i], " \n"[i == n]);
for (int i = 1; i <= n; i++)
printf("%d%c", ansd[i], " \n"[i == n]);
}

牛客练习赛43D(贪心)的更多相关文章

  1. 牛客练习赛43D Tachibana Kanade Loves Sequence

    题目链接:https://ac.nowcoder.com/acm/contest/548/D 题目大意 略 分析 贪心,首先小于等于 1 的数肯定不会被选到,因为选择一个数的代价是 1,必须选择大于1 ...

  2. [堆+贪心]牛客练习赛40-B

    传送门:牛客练习赛40 题面: 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i 个任务需要花费  x_i 的时间,同时完成第 i 个任务的时间不 ...

  3. 牛客练习赛51 **E-数列** (二分,贪心,构造)

    牛客练习赛51 E-数列 链接:https://ac.nowcoder.com/acm/contest/1083/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  4. 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)

    牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  5. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  6. 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B

    牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

  7. 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A

    牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...

  8. 最小生成树--牛客练习赛43-C

    牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 ​ 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...

  9. 牛客练习赛28-B(线段树,区间更新)

    牛客练习赛28 - B 传送门 题目 qn姐姐最好了~ ​ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, ​ 1 l r 询问区间[l,r]内的元素和 ​ 2 l r 询问区间[l,r]内的 ...

随机推荐

  1. 【LeetCode】013. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. HDOJ1114(完全背包)

    #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const ...

  3. Framework配置错误

    转自:http://blog.csdn.net/ked/article/details/25052955 VS2012命令提示符无法使用的解决方法 打开VS2012命令提示符时报错“ERROR: Ca ...

  4. 谷歌浏览器的input自动填充出现黄色背景解决方案(在已经输入内容之后)

    当你之前提交过表单,再次获取input焦点时,会有一个记录之前填写过的文本的下拉列表式的自动填充效果且带有黄色背景, 这个填充功能本身是没什么问题的,但是谷歌浏览器给了个莫名其妙的黄色背景,用css样 ...

  5. 在VirtualBox中安装CentOS 7【转载】

    当初接触Linux的时候,因为条件限制,只能在VirtualBox虚拟机中安装Linux系统使用,由于是小白,爬了好多坑.于是决定写一篇关于在虚拟机中安装linux系统的文章.一是为了巩固自己的知识, ...

  6. webrower + CEF

    理解WebKit和Chromium: Content API和CEF3 标签:               apiAPIAPibrowserchromeChromehtml5HTML5Html5web ...

  7. k8s 基础 docker-ce 安装(注k8s 的安装需要用此版docker 否则会报错 )

    yum install -y yum-utils yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/ ...

  8. 大数据学习路线copy自淘宝

    一.hadoop视频学习(入门到精通) 二.数据挖掘(入门到精通) 三.Hadoop学习路线 1.开发前期准备 首先,如果你没有Java和Linux基础,建议你先简单学一下这两门课程,此宝贝里面都为你 ...

  9. jprofiler9.1.1 安装与配置

    一.安装部分 安装包: 1.jprofiler_linux_9_1_1.rpm 2.jprofiler_windows-x64_9_1_1.exe 需要注意的是,Linux 和 Windows 安装的 ...

  10. C++中指向对象的常指针和指向常对象的指针

    指向对象的常指针 将指向对象的指针变量声明为const型,并使之初始化,这样指针值始终保持为其初始值,不能改变. Time t1(10,12,15),t2; Time * const ptr1=&am ...