• Codeforces Round #526 (Div. 2) A.B


    A. The Fair Nut and Elevator

    题目链接:https://codeforces.com/contest/1084/problem/A

    题意:

    一栋房子有n层楼,同时有个电梯(每次只能载一个人),每层楼都有ai个人。

    当电梯从x层到y层时,花费电力|x-y|。

    现在要求当电梯位于哪一层时,所有人上下两次费用最少。电梯每次到一层后若电梯里面没人会回到我们选定的楼层。

    题解:

    枚举电梯位于的层数模拟一下就好了。

    代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 105;
    int a[N];
    int n;
    
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        int ans = 99999999;
        for(int x=1;x<=n;x++){
            int sum = 0;
            for(int i=1;i<=n;i++)
                sum+=(abs(x-i)+i+x-2)*a[i]*2;
            ans=min(ans,sum);
        }
        printf("%d",ans);
        return 0;
    }

    B. Kvass and the Fair Nut

    题目链接https://codeforces.com/contest/1084/problem/B

    题意:

    给出n个桶,每个桶里面都有一定高度的东西,现在要从里面总共减少s的高度。问最后桶里面最小高度的最大值是多少。当桶里面高度都为0而s为正时,则输出-1。

    题解:

    一开始听他们说是二分,的确二分也可以。但这题没这个必要,直接贪心就好了。

    首先我们肯定减少高度较高的高度,当所有高度都一样时,这时就n个n个地减少,这样即可保证高度最小值最大。

    代码如下:

    #include <bits/stdc++.h>
    #define INF 9999999999999999
    using namespace std;
    typedef long long ll;
    const int N = 1005;
    ll n,s,minx=INF;
    ll v[N];
    int main(){
        cin>>n>>s;
        for(int i=1;i<=n;i++) scanf("%I64d",&v[i]),minx=min(minx,v[i]);
        ll sum=0;
        for(int i=1;i<=n;i++) sum+=(v[i]-minx);
        if(sum>=s){
            cout<<minx;
            return 0;
        }
        s-=sum;
        ll now = s/n+(s%n!=0);
        ll ans = minx-now;
        if(ans>=0) cout<<ans;
        else cout<<-1;
        return 0;
    }

    The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.

    It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:

    • Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
    • Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
    • Moves from the bb-th floor back to the xx-th.

    The elevator never transposes more than one person and always goes back to the floor xx before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the aa-th floor to the bb-th floor requires |ab||a−b|units of electricity.

    Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.

  • 相关阅读:
    (转)PHP 的 __FILE__ 常量
    smarty半小时快速上手教程(转)
    Namespace declaration statement has to be the very first
    ThinkPhp3.2 无法加载模块:Index
    注意mysql中的编码格式和php中的编码格式一致
    MySQL数据库备份与恢复方法(转)
    phpmyadmin设置id自增(AUTO_INCREMENT)(转)
    (转)用eclipse创建一个j2ee的web工程后,左面projects窗口中的项目如何没有显示webRoot文件夹,除了src的文件夹,其他都不显示
    CentOS6.8 安装 Nginx
    Eclipse Java注释模板设置详解
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10116342.html
Copyright © 2020-2023  润新知