牛客练习赛38 E 出题人的数组 2018ccpc桂林A题 贪心
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题 贪心的更多相关文章
- 牛客练习赛38 D 出题人的手环
链接 [https://ac.nowcoder.com/acm/contest/358/D] 题意 链接:https://ac.nowcoder.com/acm/contest/358/D 来源:牛客 ...
- 牛客练习赛42 C 出题的诀窍 (贡献,卡常)
牛客练习赛42 C 出题的诀窍 链接:https://ac.nowcoder.com/acm/contest/393/C来源:牛客网 题目描述 给定m个长为n的序列a1,a2,-,ama_1 , a_ ...
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
- 出题人的女装(牛客练习赛38题B) (概率+分式运算)
链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
- 牛客练习赛42 C 出题的诀窍
题目链接:https://ac.nowcoder.com/acm/contest/393/C 这个题就是对于每个数算贡献,如果有相同的数,只计算先出现的数的贡献 对于数x,若它在前i行的数目分别为a1 ...
- 牛客练习赛52 B Galahad (树状数组)
题目链接:https://ac.nowcoder.com/acm/contest/1084/B 题意 5e5的区间,5e5个询求[l,r]区间内出现过的数的和 思路 1s时限,莫队显然会T 我们可以将 ...
- 最小生成树--牛客练习赛43-C
牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
随机推荐
- python整形及浮点型求余数的区别
1.代码如下 a=7.0b=4.0c=7e=4 #整形求余print("%d/%d=%d" %(c,e,c/e)) #将浮点型强制转换为整形,余数用浮点型表示print(" ...
- 深入学习 Intellij IDEA 调试技巧
程序员的日常工作除了写代码之外,很大一部分时间将会在查找 BUG,解决问题.查找 BUG,离不开在 IDE 中调试代码.熟练的掌握调试技巧,可以帮助我们减少查找时间,快速定位问题. 在 IDEA 中调 ...
- cesium学习——cesium中的坐标
一.坐标展现形式 在cesium中,对于坐标数值单位有三种:角度.弧度和坐标值 1.角度 角度就是我们所熟悉的经纬度,对于地球的坐标建立如下: 图中以本初子午线作为x和z的面,建立了一个空间坐标系.可 ...
- RocketMQ中Broker的启动源码分析(二)
接着上一篇博客 [RocketMQ中Broker的启动源码分析(一)] 在完成准备工作后,调用start方法: public static BrokerController start(Broker ...
- CSS: hack 方式一览
本文引自:http://blog.csdn.net/freshlover/article/details/12132801 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...
- virtualenv使用和virtualenvwrapper使用笔记
virtualenv使用笔记 1.安装 pip install virtualenv 2.创建虚拟环境 virtualenv env //对于python2.7,该虚拟环境env必须在英文目录路径下 ...
- SQL TRUNCATE TABLE 命令
SQL TRUNCATE TABLE 命令 SQL TRUNCATE TABLE 命令用于删除现有数据表中的所有数据. 你也可以使用 DROP TABLE 命令来删除整个数据表,不过 DROP TAB ...
- 使用.Net Core CLI命令dotnet new创建自定义模板
文章起源来自一篇博客:使用 .NET CORE 创建 项目模板,模板项目,Template - DeepThought - 博客园 之前使用Abp的时候就很认同Abp创建模板项目的方式.想不到.Net ...
- AVL树(二叉平衡树)详解与实现
AVL树概念 前面学习二叉查找树和二叉树的各种遍历,但是其查找效率不稳定(斜树),而二叉平衡树的用途更多.查找相比稳定很多.(欢迎关注数据结构专栏) AVL树是带有平衡条件的二叉查找树.这个平衡条件必 ...
- .NET Core C#中级篇2-5 常见实用类
.NETCore CSharp 中级篇2-5 本节内容为常见实用类和方法的使用 String.Format string.format方法是一个字符串格式化类,它里面的一些写法是对字符串进行指定格式的 ...