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 = ;
int a[N];
int n; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int ans = ;
for(int x=;x<=n;x++){
int sum = ;
for(int i=;i<=n;i++)
sum+=(abs(x-i)+i+x-)*a[i]*;
ans=min(ans,sum);
}
printf("%d",ans);
return ;
}

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 = ;
ll n,s,minx=INF;
ll v[N];
int main(){
cin>>n>>s;
for(int i=;i<=n;i++) scanf("%I64d",&v[i]),minx=min(minx,v[i]);
ll sum=;
for(int i=;i<=n;i++) sum+=(v[i]-minx);
if(sum>=s){
cout<<minx;
return ;
}
s-=sum;
ll now = s/n+(s%n!=);
ll ans = minx-now;
if(ans>=) cout<<ans;
else cout<<-;
return ;
}

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 |a−b||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.

Codeforces Round #526 (Div. 2) A.B的更多相关文章

  1. [Codeforces Round #526 (Div. 2)]

    https://codeforces.com/contest/1084 A题 数据量很小,枚举就行 #include<iostream> #include<cstdio> #i ...

  2. Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings

    E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...

  3. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

    D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...

  4. Codeforces Round #526 (Div. 2) C. The Fair Nut and String

    C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...

  5. Codeforces Round #526 Div. 1 自闭记

    日常猝死. A:f[i]表示子树内包含根且可以继续向上延伸的路径的最大价值,统计答案考虑合并两条路径即可. #include<iostream> #include<cstdio> ...

  6. Codeforces Round #526 (Div. 2) Solution

    A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...

  7. Codeforces Round #526 (Div. 1)

    毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...

  8. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  9. A. The Fair Nut and Elevator (Codeforces Round #526 (Div. 2))

    A. The Fair Nut and Elevator 好笨啊QAQ. 暴力枚举的题,连分类都不用. 从电梯初始位置到第一层.人到第一层.间隔的层数,往返路程. #include <bits/ ...

随机推荐

  1. Python学习手册之控制结构(一)

    在上一篇文章中,我们对 Python 进行了简单介绍和介绍了 Python 的基本语法,现在我们继续介绍 Python 控制结构. 查看上一篇文章请点击:https://www.cnblogs.com ...

  2. Ruby字符串的一些方法

    最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...

  3. Angularjs 跨域post数据到springmvc

    先贴网上己有解决方案链接: http://www.tuicool.com/articles/umymmqY  (讲的是springmvc怎么做才可以跨域) http://my.oschina.net/ ...

  4. python2.7练习小例子(二十二)

        22):题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和.     程序分析:请抓住分子与分母的变化规律. #!/usr/bin/pyt ...

  5. 初步学习pg_control文件之十二

    接前问,初步学习pg_control文件之十一,再来看下面这个 XLogRecPtr minRecoveryPoint; 看其注释: * minRecoveryPoint is updated to ...

  6. shell eval命令使用

    eval命令将会首先扫描命令行进行所有的置换,然后再执行该命令. 该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两次扫描. 这些需要进行两次扫描的变量有时被称为复杂变量.不过这些变量本 ...

  7. itop-4412开发板学习-内核信号量

    1. 翻翻书看下,linux提供两种信号量,内核信号量,由内核控制路径使用,System V IPC信号量,由用户态进程使用.下面的就是内核部分的信号量.内核信号量类似于自旋锁,当锁关闭着时,不允许内 ...

  8. Cyclone IV器件的逻辑单元和逻辑阵列快

    1. 逻辑单元 (LE) 在 Cyclone IV 器件结构中是最小的逻辑单位.LE 紧密且有效的提供了高级功能的逻辑使用.每个 LE 有以下特性:一个四口输入的查找表 (LUT),以实现四种变量的任 ...

  9. 【MVC】 小问题

    [MVC] 小问题 1. url 传参中文乱码 : encodeURIComponent 转码 2. RedirectToAction 重定向 : ajax 调用无效, 直接 url 访问有效 3. ...

  10. Returning Values from Bash Functions

    转自:https://www.linuxjournal.com/content/return-values-bash-functions Bash functions, unlike function ...