CF#354 A. Vasya and Robot(思维题)
2017-03-29 12:33
435 查看
A. Vasya and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in
such a way that the leftmost item has number 1, the rightmost item has number n.
Each item has a weight, the i-th item weights wi kilograms.
Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the righ
4000
t one. The robot can consecutively perform the following actions:
Take the leftmost item with the left hand and spend wi · l energy
units (wi is
a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy
units;
Take the rightmost item with the right hand and spend wj · r energy
units (wj is
a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy
units;
Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.
Input
The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).
Output
In the single line print a single number — the answer to the problem.
Examples
input
output
input
output
Note
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last
item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.
The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy
units.
题意:有一排重量不等的物品,每次只能用左手拿最左边的物品或右手拿最右边的物品,并花费l或者r乘上重量w的能量,使用左右手次数没有限制,但只要连续两次使用同一只手就会有额外的花费(连续两次用左手多花费Ql点能量,连续两次用右手多花费Qr点能量)
思路:因为我们只能用左手拿左边的物品,右手拿右边的物品,所以我们只需枚举出一个断点,断点左侧所有物品由左手拿,右侧物品由右手拿,再计算左右手拿的次数之差,乘上相应的Ql,Qr即多花费的能量。
需要注意的是,我们要考虑每个断点时左手先拿和右手先拿的情况,注意细节。
题中数据范围为1e5,我们需要用前缀数组和后缀数组优化计算断点左右物品重量的操作,时间复杂度为 O(n)。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in
such a way that the leftmost item has number 1, the rightmost item has number n.
Each item has a weight, the i-th item weights wi kilograms.
Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the righ
4000
t one. The robot can consecutively perform the following actions:
Take the leftmost item with the left hand and spend wi · l energy
units (wi is
a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy
units;
Take the rightmost item with the right hand and spend wj · r energy
units (wj is
a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy
units;
Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.
Input
The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).
Output
In the single line print a single number — the answer to the problem.
Examples
input
3 4 4 19 1 42 3 99
output
576
input
4 7 2 3 9 1 2 3 4
output
34
Note
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last
item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.
The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy
units.
题意:有一排重量不等的物品,每次只能用左手拿最左边的物品或右手拿最右边的物品,并花费l或者r乘上重量w的能量,使用左右手次数没有限制,但只要连续两次使用同一只手就会有额外的花费(连续两次用左手多花费Ql点能量,连续两次用右手多花费Qr点能量)
思路:因为我们只能用左手拿左边的物品,右手拿右边的物品,所以我们只需枚举出一个断点,断点左侧所有物品由左手拿,右侧物品由右手拿,再计算左右手拿的次数之差,乘上相应的Ql,Qr即多花费的能量。
需要注意的是,我们要考虑每个断点时左手先拿和右手先拿的情况,注意细节。
题中数据范围为1e5,我们需要用前缀数组和后缀数组优化计算断点左右物品重量的操作,时间复杂度为 O(n)。
#include<bits/stdc++.h> using namespace std; const int N = 1e5+1; int a ,prefix ,suffix ; int abs(int a) { if(a>=0) return a; else return -a; } int main() { int n,l,r,ll,rr; while(scanf("%d%d%d%d%d",&n,&l,&r,&ll,&rr)==5) { int mmin=1<<30,sum=0; ///此处mmin=0x3f3f3f还是太小 for(int i=1,sum=0; i<=n; i++) ///坑在这好多发。。。 { scanf("%d",&a[i]); sum+=a[i]; prefix[i]=sum; } for(int i=n,sum=0; i>0; i--) { sum+=a[i]; suffix[i]=sum; } suffix[n+1]=0,prefix[0]=0; for(int i=0,t; i<=n; i++) { sum=prefix[i]*l+suffix[i+1]*r; if(abs(i*2-n)<=1) t=0; else if(i>n-i) t=ll*(i*2-n-1); else t=rr*(n-2*i-1); if(sum+t<mmin) mmin=sum+t; } printf("%d\n",mmin); } }
相关文章推荐
- CodeForces 354A - Vasya and Robot (简单思维)
- [CF355C]Vasya and Robot(思维,贪心)
- A. Vasya and Robot----思维题
- CodeForces 354A - Vasya and Robot 简单思维..赋初值一定不能随便了T_T
- codeforces 355C - Vasya and Robot
- Codeforces 355C Vasya and Robot【贪心+前缀和】
- Codeforces Round #206 (Div. 2) C-Vasya and Robot
- Vasya and Robot
- Codeforces 752C Santa Claus and Robot 思维
- 数学 之 Codeforces 354A - Vasya and Robot
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- 数学 之 Codeforces 354A - Vasya and Robot
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- Codeforces Round #206 (Div. 2) C. Vasya and Robot
- Codeforces 355C Vasya and Robot 暴力枚举
- codeforce C - Vasya and Robot
- Codeforces Round #206 (Div. 2) C - Vasya and Robot
- 【CF1073C】Vasya and Robot(二分,构造)
- Codeforces Round #206 (Div. 1) A. Vasya and Robot