https://ac.nowcoder.com/acm/contest/358/E

题意:

出题人有两个数组,A,B,请你把两个数组归并起来使得cost=∑i∗ci 最小,归并要求原数组的数的顺序在新数组中不改变。

题解:

先分别处理A和B数组,把A先分成n段,把某段均值大于前面的一段,就把这两段合并。处理完A,B段后就可以取大原则归并。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = 1e5+;
int A[maxn],B[maxn]; struct node{
ll sum;int cnt; friend bool operator < (node a, node b){
return a.sum * b.cnt < b.sum * a.cnt;
}
friend node operator + (node a,node b){
return (node) {a.sum + b.sum, a.cnt + b.cnt};
}
}S[maxn],T[maxn];
int C[maxn*]; void add(int op, int l,int r,int &now){
for(int i=l; i<=r; i++){
if(op==) C[now++] = A[i];
else C[now++] = B[i];
}
}
int main(){
int n,m; scanf("%d%d", &n, &m); for(int i=; i<=n; i++) {
scanf("%d", &A[i]);
}
for(int i=; i<=m; i++){
scanf("%d", &B[i]);
}
int tot1 = ;
for(int i=; i<=n; i++){
S[++tot1] = (node) {A[i], };
while(tot1> && S[tot1-] < S[tot1]){
S[tot1-] = S[tot1-] + S[tot1];
tot1--;
}
} int tot2 = ; for(int i=; i<=m; i++){
T[++tot2] = (node) {B[i], };
while(tot2 > && T[tot2-] < T[tot2]){
T[tot2-] = T[tot2-] + T[tot2];
tot2--;
}
} S[++tot1] = (node){-, };
T[++tot2] = (node){-, }; int L=,R=;
int prel=,prer=,now=;
while(L < tot1 || R < tot2){
if(S[L] < T[R]){
add(, prer,prer+T[R].cnt-,now);
prer += T[R].cnt;
R++;
}
else {
add(, prel,prel+S[L].cnt-,now);
prel += S[L].cnt;
L++;
}
}
ll ans = ;
for(int i=; i<=n+m;i++){
ans += 1ll*i*C[i];
// cout<<C[i]<<" ";
}
// cout<<endl;
printf("%lld\n", ans);
return ;
}

牛客练习赛38 E 出题人的数组 2018ccpc桂林A题 贪心的更多相关文章

  1. 牛客练习赛38 D 出题人的手环

    链接 [https://ac.nowcoder.com/acm/contest/358/D] 题意 链接:https://ac.nowcoder.com/acm/contest/358/D 来源:牛客 ...

  2. 牛客练习赛42 C 出题的诀窍 (贡献,卡常)

    牛客练习赛42 C 出题的诀窍 链接:https://ac.nowcoder.com/acm/contest/393/C来源:牛客网 题目描述 给定m个长为n的序列a1,a2,-,ama_1 , a_ ...

  3. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  4. 出题人的女装(牛客练习赛38题B) (概率+分式运算)

    链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  5. 牛客练习赛42 C 出题的诀窍

    题目链接:https://ac.nowcoder.com/acm/contest/393/C 这个题就是对于每个数算贡献,如果有相同的数,只计算先出现的数的贡献 对于数x,若它在前i行的数目分别为a1 ...

  6. 牛客练习赛52 B Galahad (树状数组)

    题目链接:https://ac.nowcoder.com/acm/contest/1084/B 题意 5e5的区间,5e5个询求[l,r]区间内出现过的数的和 思路 1s时限,莫队显然会T 我们可以将 ...

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

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

  8. 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)

    牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...

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

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

随机推荐

  1. codeforces 339 D.Xenia and Bit Operations(线段树)

    这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了. 题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然 ...

  2. win10去除快捷方式小箭头

    切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...

  3. RabbitMQ(四):使用Docker构建RabbitMQ高可用负载均衡集群

    本文使用Docker搭建RabbitMQ集群,然后使用HAProxy做负载均衡,最后使用KeepAlived实现集群高可用,从而搭建起来一个完成了RabbitMQ高可用负载均衡集群.受限于自身条件,本 ...

  4. 数据结构-二叉搜索树和二叉树排序算法(python实现)

    今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...

  5. Altium Designer16设置GND和VCC线宽规则的一种操作方法及注意事项

    昨天看到学弟在画电路板,看到他设置电源线线宽时出了一点问题,设置的规则最开始有作用,后来重新从原理图导入更新PCB时,电源线变绿,规则设置点更新也没有用.接下来是操作步骤: 第一步:点击Design- ...

  6. Why do I write a blog

    I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...

  7. Mermaid

    graph TD; A-->B; A-->C; B-->D; C-->D;

  8. jQuery学习和知识点总结归纳

    jQuery目前在Web前端开发所占的比重越来越高,在我们jQuery学习和开发的过程中都会去使用.jQuery帮我们解决了浏览器之间JS一些不兼容的地方和简化了原生JS对DOM的操作.下面把PHP程 ...

  9. android——卡片式布局

    一.CardView <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk ...

  10. ASP.NET Core on K8S深入学习(3-2)DaemonSet与Job

    本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇<3-1 Deployment>中介绍了Deployment ...