uva 11054 Gerovia的酒交易(贪心+树状数组)

直线上有n个等距的村庄,每个村庄要么买酒,要么卖酒。把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力。问最少需要多少劳动力才能满足所有村庄的需求

思路贪心

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std;  

const int maxn = 100000 + 100;
const int INF = 0x3f3f3f3f;
//freopen("input.txt", "r", stdin);
int C[maxn], n;

int lowbit(int x) { return x&(-x);}
int sum(int x) {
	if(x == 0) return 0;
	int ret = 0;
	while(x > 0) {
		ret += C[x]; x -= lowbit(x);
	}
	return ret;
}
void add(int x, int d) {
	while(x <= n) {
		C[x] += d; x += lowbit(x);
	}
}

LL solve(int left, int right) {
	if(left == right) return (LL)0;
	if(left+1 == right) return (LL)abs(sum(right)-sum(left));
	int mid = (left + right) >> 1;
	int tmp = sum(mid) - sum(left-1);
	add(mid, -tmp); add(mid+1, tmp);
	return solve(left, mid) + solve(mid+1, right) + (LL)abs(tmp);
}

void init() {
	memset(C, 0, sizeof(C));
	int tmp;
	for(int i = 1; i <= n; i++) {
		cin >> tmp;
		add(i, tmp);
	}
}

int main() {
	//freopen("input.txt", "r", stdin);
	while(scanf("%d", &n) == 1 && n) {
		init();
		cout << solve(1, n) << endl;
	}
	return 0;
}
时间: 2024-10-30 23:20:35

uva 11054 Gerovia的酒交易(贪心+树状数组)的相关文章

UVA 11990 `Dynamic&#39;&#39; Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3141 题意 一个1到n的排列,每次随机删除一个,问删除前的逆序数 思路 综合考虑,对每个数点,令value为值,pos为位置,time为出现时间(总时间-消失时间),明显是统计value1 > value2, pos1 < pos2, time1 < time2的个

[luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)

传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include <queue> #include <cstdio> #include <iostream> #define N 100001 #define ls now << 1 #define rs now << 1 | 1 #define max(x, y) (p[

D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个问题: 数轴上有个点突然兴奋,如果自己被身上覆盖了超过条线段,这个点就会浑身难受然后把Koishi批判一番. Koishi十分善良,为了不让数轴上的点浑身难受,也为了让自己开心,她想在数轴上放入尽量多的线段. 按照套路,Koishi假装自己并不会做这道题,所以她就来求你帮忙.并承诺如果你解决了问题就

UVALive 6911---Double Swords(贪心+树状数组(或集合))

题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4923 problem  description Last night, Kingdom of Light was attacked by Kingdom of Dark! The queen of Kingdom of Light, Queen Ar, was ca

Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)

George is a cat, so he loves playing very much. Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers f

【HOJ2430】【贪心+树状数组】 Counting the algorithms

As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of the algorithms quickly. After a short time, he has learned a lot. One day, mostleg asked him that how many he had learned. That was really a hard proble

[CSP-S模拟测试]:d(贪心+树状数组)

题目传送门(内部题65) 输入格式 第一行,一个自然数$T$,代表数据组数.对于每组数据:第一行,一个正整数$n$,一个自然数$m$.接下来$n$行,每行两个正整数,$a_i,b_i$. 输出格式 对于每组数据,输出一行,一个整数,代表答案. 样例 样例输入: 3 2 0 5 10 5 5 2 1 1 1 2 2 3 1 3 5 4 4 5 3 样例输出: 25412 数据范围与提示 保证$0\leqslant m<n,a_i,b_i\leqslant 10^5$. 题解 题目并不难,考虑贪心,

UVa 11054 Gergovia的酒交易

https://vjudge.net/problem/UVA-11054 题意:直线上有n个等距的村庄,每个村庄要么买酒,要么卖酒.设第i个村庄对酒的需求为ai,ai>0表示买酒,ai<0表示卖酒,所有村庄供需平衡.把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力.计算所需最少劳动力. 思路:从左边第一个开始分析,如果它卖酒,则可以把它全卖给第二个村庄,如果它买酒,可以从第二个村庄那里买酒,依次下去分析第二个直到最后一个村庄.这样的话每次买酒和卖酒的距离都是最短的,劳动力肯定也是最少的

UVA 11054 Gergovia的酒交易 Wine trading in Gergovia

一道思维题,主要用到的方法是思路转化. 首先考虑第一个村庄,首先他本身一定会用|a[1]|的劳动力 a[1]>0 那么2村庄一定要花a[1]的劳动力来运输,也就是需要a[1]+a[2]的劳动力 a[1]<0 那么2村庄就已经有-a[1]瓶酒,就可以不用去买了,也就是a[2]-(-a[1])=a[1]+a[2] 综上,我们通过扫描法来记录前几个村庄带给这个村庄的酒,累加劳动力就好了 #include<bits/stdc++.h> using namespace std; int n;