uva 11054 - Wine trading in Gergovia

点击打开链接uva 11054

题目意思:    有一个城市,城市里的每一个人都在做酒生意,有的人是要买进用正数表示,有的人是要卖出用负数表示。现在规定这个城市的每家每户都连在一起,还有L升酒的交易代价为“两家的距离xL“,要求我们找到最小的交易代价

解题思路:     1:贪心
                       2:由于每一个人要买的或要卖的物品是一定的,那么我们知道如果要让每一个人的产生最小的交易代价就是让每一个人都和他相邻的人交易,那么这样就有最优解,所以只要从左向右枚举一遍就是O(n)的时间复杂度

代码:

1

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
#define MAXN 100010

int n;
int bott[MAXN];
long long ans;

void solve() {
    int i;
    ans = 0;
    for(i = 1 ; i < n ; i++){
        bott[i] += bott[i-1];
        ans += abs(bott[i-1]);
    }
    printf("%lld\n" , ans);
}

int main() {
    //freopen("input.txt" , "r" , stdin);
    while(scanf("%d" , &n) && n){
        for(int i = 0 ; i < n ; i++)
            scanf("%d" , &bott[i]);
        solve();
    }
    return 0;
}

2

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
#define MAXN 100010

int n;
int bott[MAXN];
long long ans;

void solve() {
    int i , j;
    for(i = 0 ; i < n ;){
        if(bott[i] < 0){
            for(j = i+1 ; j < n ; j++){
                if(bott[j] > 0){
                    if(abs(bott[i]) > abs(bott[j])){
                        ans += abs(bott[j])*(j-i);
                        bott[i] += bott[j] ; bott[j] = 0;
                    }
                    else{
                        ans += abs(bott[i])*(j-i);
                        bott[j] += bott[i] ; bott[i] = 0;
                    }
                    break;
                }
            }
        }
        else if(bott[i] > 0){
            for(j = i+1 ; j < n ; j++){
               if(bott[j] < 0){
                    if(abs(bott[i]) > abs(bott[j])){
                        ans += abs(bott[j])*(j-i);
                         bott[i] += bott[j] ; bott[j] = 0;
                    }
                    else{
                        ans += abs(bott[i])*(j-i);
                        bott[j] += bott[i] ; bott[i] = 0;
                    }
                    break;
                }
            }
        }
        if(!bott[i])  i++;
    }
}

int main() {
    //freopen("input.txt" , "r" , stdin);
    while(scanf("%d" , &n) && n){
        for(int i = 0 ; i < n ; i++)
            scanf("%d" , &bott[i]);
        ans = 0 ; solve();
        printf("%lld\n" , ans);
    }
    return 0;
}
时间: 2024-11-09 00:08:54

uva 11054 - Wine trading in Gergovia的相关文章

UVa 11054:Wine trading in Gergovia

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1995 [原题] As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of one street, and

UVa 10700 Camel trading:计算表达式

10700 - Camel trading Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formul

uva 10700 - Camel trading

点击打开链接 题目意思:     给定一个表达式,要求找到这个表达式的最大值和最小值 解题思路:     1:思路:模拟题                       2:对于给定的一个表达式,最小值就是直接去计算这个表达式.如果要求算出的值最大,那么我们知道乘号的个数是不会改变的,所以如果能够让乘号旁边的数字越大越好,所 以我们把所有+旁边的数全部加为一个数,然后在计算就是最大值,例如3+11+4*1*13*12*8+3*3+8应该就是要(3+11+4)*(1)*(13)*(12)*(8+3)

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the

算法题:UVA 10280 Old Wine Into New Bottles(dp完全背包)

Problem C: Old Wine Into New Bottles Wine bottles are never completely filled: a small amount of air must be left in the neck to allow for thermal expansion and contraction. If too little air is left in the bottle, the wine may expand and expel the c

UVa 10721 Bar Codes (DP)

10721 - Bar Codes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1662 A bar-code symbol consists of alternating dark and light bars, starting with a dark

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c