题目链接:https://ac.nowcoder.com/acm/contest/548/D

题目大意

  略

分析

  贪心,首先小于等于 1 的数肯定不会被选到,因为选择一个数的代价是 1,必须选择大于1的数答案才有可能增加。

  其次,答案一定是在某一个数组所有大于 1 的元素全部选完的情况下得出来的,可以用反证法证明。

  对于 a, b 两个数组,我们可以考虑先全部选中所有大于 1 的元素,然后从那个和比较大的数组中不断扣掉数,在两数组和的大小关系不变的情况下极限扣数,扣完答案就出来了,扣数也是贪心,从小的数开始扣才能扣最多。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int n, a[maxN], b[maxN], c[maxN], d[maxN];
LL sumA, sumB; // 保存对应a[i]*c[i] , b[i]*d[i]的累加和
int cntC, cntD; // 保存 c, d中1的个数 int main(){
INIT();
cin >> n;
Rep(i, n) {
cin >> a[i];
// 先把大于1的全部选中,且只有大于1的才有必要选
if(a[i] > ) {
c[i] = ;
++cntC;
sumA += a[i];
}
}
Rep(i, n) {
cin >> b[i];
if(b[i] > ) {
d[i] = ;
++cntD;
sumB += b[i];
}
} // A指向和比较大的序列数组
// B指向对应构造的序列数组
// cntB指向记录B中有多少个1的变量
int *A, *B;
int* cntB;
// 最小堆
priority_queue< PII, vector< PII >, greater< PII > > pQ;
LL target = abs(sumA - sumB);
if(sumA > sumB) {
A = a;
B = c;
cntB = &cntC;
}
else {
A = b;
B = d;
cntB = &cntD;
}
// 排序太烦了,直接放堆里
Rep(i, n) if(B[i] == ) pQ.push(MP(A[i], i)); // 从小到大取,看看最多能从和比较大的数组中取走多少个
while(!pQ.empty()) {
PII tmp = pQ.top();
pQ.pop(); target -= tmp.ft;
if(target >= ) {
B[tmp.sd] = ;
--*cntB;
}
else break;
} cout << min(sumA, sumB) - cntC - cntD << endl;
Rep(i, n) cout << c[i] << " ";
cout << endl;
Rep(i, n) cout << d[i] << " ";
cout << endl;
return ;
}

牛客练习赛43D Tachibana Kanade Loves Sequence的更多相关文章

  1. 牛客练习赛43 Tachibana Kanade Loves Game (简单容斥)

    链接:https://ac.nowcoder.com/acm/contest/548/F来源:牛客网 题目描述 立华奏是一个天天打比赛的萌新. 省选将至,萌新立华奏深知自己没有希望进入省队,因此开始颓 ...

  2. 牛客练习赛43 Tachibana Kanade Loves Review C(最小生成树Kruskal)

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

  3. 牛客练习赛43 Tachibana Kanade Loves Probability(快速幂)

    链接:https://ac.nowcoder.com/acm/contest/548/B来源:牛客网 题目描述 立华奏在学习初中数学的时候遇到了这样一道大水题: “设箱子内有 n 个球,其中给 m 个 ...

  4. 牛客练习赛43F Tachibana Kanade Loves Game

    题目地址 Link 题解 这题其实就是求1~n中有多少与2~20互质的数,然后其实只跟1~20里面的质数有关. 那么考虑容斥一下求出来一共有多少个不互质的,用n减一下就是互质的数的个数了.然后判一下a ...

  5. 牛客练习赛43C Tachibana Kanade Loves Review

    题目地址 Link 题解 虚点这种东西还是没有掌握好啊. 考虑建一个虚点,向已经学会的东西连一条边权为0的边,关系正常连边,单独学的从虚点连一条边过去. 然后做一遍最小生成树就得到答案了. 这题略卡常 ...

  6. 牛客练习赛43B Tachibana Kanade Loves Probability

    题目链接:https://ac.nowcoder.com/acm/contest/548/C 题目大意 略 分析 利用快速幂先移到 k1 位,然后开始一个一个取余数. 代码如下 #include &l ...

  7. 牛客练习赛 43 B-Tachibana Kanade Loves Probability

    链接:https://ac.nowcoder.com/acm/contest/548/B 题目描述 立华奏在学习初中数学的时候遇到了这样一道大水题: “设箱子内有 n 个球,其中给 m 个球打上标记, ...

  8. 牛客练习赛43D(贪心)

    有生之年我居然也能不看题解做出来题QAQ-- 发现c.d是0.1序列而不是随机数列说明有蹊跷,于是发现负数直接配0,正数配1即可.不知道哪个最小,那就全求一下吧--我的做法的坑点是数正好为1时不可以选 ...

  9. 牛客网 牛客练习赛43 F.Tachibana Kanade Loves Game-容斥(二进制枚举)+读入挂

    链接:https://ac.nowcoder.com/acm/contest/548/F来源:牛客网 Tachibana Kanade Loves Game 时间限制:C/C++ 1秒,其他语言2秒 ...

随机推荐

  1. JAVA并发工具类---------------(CountDownLatch和CyclicBarrier)

    CountDownLatch是什么 CountDownLatch,英文翻译为倒计时锁存器,是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 闭锁可以延迟线程的进 ...

  2. Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据

    Python Elasticsearch api   描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下 ...

  3. 利用mysql数据库日志文件获得webshell

    查看配置 show variables like '%general%'; 开启日志功能 set GLOBAL general_log='ON'; 设置日志存储路径 SET GLOBAL genera ...

  4. ArcGis 字段计算器表达式(Field calculator expression).cal文件与标注表达式(label expression).lxp的实质及其编码方式、解析方法

    ArcGis 字段计算器表达式可以保存为一个扩展名为.cal的文件,该文件的实质是一个文本文件,编码方式为 UTF-16-LE. 官方的说明是“ArcGIS 应用程序使用 UTF-16-LE 编码读写 ...

  5. 尝试修改源码需要用到git存一下

    git reflog查看本地记录 git reset --hard 02a3260

  6. spring Aop设计原理

    转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...

  7. 22-1-sort

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Spring入门(四)Spring-test模块

    自动化转配bean的测试案例分析 package soundsystem; import static org.junit.Assert.*; import org.junit.Rule; impor ...

  9. JVM配置参数理解,Cannot load this JVM TI agent twice

    基本参数 -Xms128m JVM初始分配的堆内存 -Xmx512m JVM最大允许分配的堆内存,按需分配 -XX:PermSize=64M JVM初始分配的非堆内存 -XX:MaxPermSize= ...

  10. 7.Struts2框架封装数据

    Struts2框架提供了很强大的数据封装的功能,不再需要使用Servlet的API完成手动封装了!! 第一种方式:属性驱动 > 提供对应属性的set方法进行数据的封装.--经常使用 * 表单的哪 ...