题目链接: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. Android中怎么破解游戏之修改金币数

    我们在玩游戏的时候总是会遇到一些东东需要进行购买的,但是我们可能又舍不得花钱,那么我们该怎么办呢?那就是用游戏外挂吧!我们这里说的是Android中的游戏,在网上搜索一下移动端游戏外挂,可能会找到一款 ...

  2. spring AOP (使用AspectJ的xml方式 的aop实现) (7)

    目录 一.定义计算器接口跟实现类 二.定义两个切面,日志切面和验证切面 三.在xml中配置切面 四.测试类 一.定义计算器接口跟实现类 public interface ArithmeticCalcu ...

  3. iptables防DDOS攻击和CC攻击配置

    防范DDOS攻击脚本 #防止SYN攻击 轻量级预防 iptables -N syn-flood iptables -A INPUT -p tcp –syn -j syn-flood iptables ...

  4. System.getenv()和System.getProperty()

    System.getenv() 方法是获取指定的环境变量的值. System.getenv(String str) 接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null. S ...

  5. C++——数据结构之链表

    直接上例子 int main() { ,,}; ,,}; Listnode *head=NULL,*temp; head=(Listnode*)malloc(sizeof(Listnode));//头 ...

  6. SPOJ MAXMATCH - Maximum Self-Matching (FFT)

    题目链接:MAXMATCH - Maximum Self-Matching Description You're given a string s consisting of letters 'a', ...

  7. mysql优化3:BTree索引和Hash索引

    一.BTree索引 注:名叫btree索引,大的方面看,都用的平衡树,但具体的实现上,各引擎稍有不同,比如,严格地说,NDB引擎使用的是T-tree,Myisam和innodb中默认用B-tree索引 ...

  8. SQL Serve2008的一些操作

    今天花了一下午的时间在熟悉SQL serve的一些操作,在此记录下学习到的东西: 首先创建数据库: use master --设置当前数据库为master,以便方便访问表sysdatabases if ...

  9. 【一】Jmeter接口自动化测试系列之参数化方法

    Jmeter作为虽然作为一款和LoadRunner相媲美的性能测试工具,但参数化功能实在不咋地,这里我大概总结了一下Jmeter的参数化方法! 至于参数化的用途,我这里就不多说了,做测试的都明白吧!本 ...

  10. [已解决]报错:ValueError: Expected 2D array, got scalar array instead

    报错代码: new_x = 84610 pre_y = model.predict(new_x) print(pre_y) 报错结果: ValueError: Expected 2D array, g ...