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. iOS程序员 如何提升核心竞争力,防止自己被裁员?

    前言: 核心竞争力最早由普拉哈拉德和加里·哈默尔两位教授提出,通常认为核心竞争力,即企业或个人相较于竞争对手而言所具备的竞争优势与核心能力差异, 说白了就是你的优势,而且最好是独一无二的的优势,这就是 ...

  2. web图形验证码逻辑

    逻辑:前端生成一个UUID以URL方式发送给后端,后端准备Redis数据库缓存数据,后端拿到UUID后,调用captcha.generate_captcha()生成图片和图片的标签,Redis数据库保 ...

  3. git的使用(一)

    git   —version  展示git的版本 tanya ~$ git --version git version 2.22.0 最小配置   git config —global user.na ...

  4. 【游记】NOIP2018初赛

    声明 本文最初的版本创建之时,本人甚至只是个电脑的小白,因而不太会用电脑编辑文字,最初的版本写在一个Word文档里,被随意的丢弃在我杂乱无比的网盘的某一个角落,直到我决定整理自己的成长历程,将散落的游 ...

  5. Java 通过反射改变私有变量的值

    直接上代码 import java.lang.reflect.Field; public class Main {      public static void main(String[] args ...

  6. android ——调用摄像头拍照和相册

    先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  7. 探究光线追踪技术及UE4的实现

    目录 一.光线追踪概述 1.1 光线追踪是什么 1.2 光线追踪的特点 1.3 光线追踪的历史 1.4 光线追踪的应用 二.光线追踪的原理 2.1 光线追踪的物理原理 2.2 光线追踪算法 2.3 R ...

  8. 面试java_后端面经_5

    情话部分: 小姐姐:为什么有很多人在感情中付出很多,却得不到想要的结果? 你答:我听过一个这样的故事:讲的是蚯蚓一家人,有一天,蚯蚓爸爸特别无聊,就把自己切成了俩段愉快的打羽毛球去了,蚯蚓妈妈见状,把 ...

  9. 本地VSCode编辑远程服务器文件

    前言 先说下我的场景:服务器搭设了一系列复杂环境,然后需要使用PHP实现某些功能 选这种远程编辑的原因: 首先PHP打死我也不想装(这个现在是出了VB外最惹人厌的语言了) 然后环境比较复杂,本地装下比 ...

  10. CAD2015 C#二次开发 字体变形

    开发环境:VS2012问题描述:一个简单的WinForm窗口,一个群组控件和一个Label,都是微软雅黑12pxCAD2015下,看起来却不一样,一个明显细得多. CAD2014下,无此问题.实验了C ...