Codeforces Round #381 (Div. 2)(A(dfs)+B(前缀和))
2016-11-25 16:59
274 查看
A. Alyona and copybooks
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook’s packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.
What is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.
Input
The only line contains 4 integers n, a, b, c (1 ≤ n, a, b, c ≤ 109).
Output
Print the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.
Examples
input
1 1 3 4
output
3
input
6 2 1 1
output
1
input
4 4 4 4
output
0
input
999999999 1000000000 1000000000 1000000000
output
1000000000
Note
In the first example Alyona can buy 3 packs of 1 copybook for 3a = 3 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally.
In the second example Alyuna can buy a pack of 2 copybooks for b = 1 ruble. She will have 8 copybooks in total.
In the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn’t need to buy anything.
In the fourth example Alyona should buy one pack of one copybook.
题意:你有n本书,三种买书方式。1本书a元,两本书b元,三本书c元。
问总书数满足是4的倍数时的最小花费。
题解:dfs当前书的数量%4,和花费。dfs次数要多,过程中记录最小值即可。
代码:
B. Alyona and flowers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.
Let’s define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl’s happiness its mood multiplied by the number of chosen subarrays the flower is in.
For example, consider the case when the mother has 5 flowers, and their moods are equal to 1, - 2, 1, 3, - 4. Suppose the mother suggested subarrays (1, - 2), (3, - 4), (1, 3), (1, - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:
the first flower adds 1·1 = 1 to the girl’s happiness, because he is in one of chosen subarrays,
the second flower adds ( - 2)·1 = - 2, because he is in one of chosen subarrays,
the third flower adds 1·2 = 2, because he is in two of chosen subarrays,
the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,
the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.
Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl’s happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!
Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother.
The second line contains the flowers moods — n integers a1, a2, …, an ( - 100 ≤ ai ≤ 100).
The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], …, a[ri].
Each subarray can encounter more than once.
Output
Print single integer — the maximum possible value added to the Alyona’s happiness.
Examples
input
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
output
7
input
4 3
1 2 3 4
1 3
2 4
1 1
output
16
input
2 2
-1 -2
1 1
1 2
output
0
Note
The first example is the situation described in the statements.
In the second example Alyona should choose all subarrays.
The third example has answer 0 because Alyona can choose none of the subarrays.
题意:给你一个长度为n的区间,和m个子区间。如果选择该子区间,和值加上子区间的和。问和最大为多少。
题解:
如果子区间的和大于0,就选,否则就不选。
代码:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook’s packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.
What is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.
Input
The only line contains 4 integers n, a, b, c (1 ≤ n, a, b, c ≤ 109).
Output
Print the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.
Examples
input
1 1 3 4
output
3
input
6 2 1 1
output
1
input
4 4 4 4
output
0
input
999999999 1000000000 1000000000 1000000000
output
1000000000
Note
In the first example Alyona can buy 3 packs of 1 copybook for 3a = 3 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally.
In the second example Alyuna can buy a pack of 2 copybooks for b = 1 ruble. She will have 8 copybooks in total.
In the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn’t need to buy anything.
In the fourth example Alyona should buy one pack of one copybook.
题意:你有n本书,三种买书方式。1本书a元,两本书b元,三本书c元。
问总书数满足是4的倍数时的最小花费。
题解:dfs当前书的数量%4,和花费。dfs次数要多,过程中记录最小值即可。
代码:
#include <bits/stdc++.h> #define ll long long using namespace std; ll mx=1e19; ll n,a,b,c; void dfs(ll x,ll ans) { if(x%4==0) mx=min(mx,ans); if(x>4*n) return ; dfs(x+1,ans+a); dfs(x+2,ans+b); dfs(x+3,ans+c); } int main() { cin>>n>>a>>b>>c; if(n%4==0) { printf("0\n"); } else { n%=4; dfs(n,0); printf("%I64d\n",mx); } }
B. Alyona and flowers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.
Let’s define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl’s happiness its mood multiplied by the number of chosen subarrays the flower is in.
For example, consider the case when the mother has 5 flowers, and their moods are equal to 1, - 2, 1, 3, - 4. Suppose the mother suggested subarrays (1, - 2), (3, - 4), (1, 3), (1, - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:
the first flower adds 1·1 = 1 to the girl’s happiness, because he is in one of chosen subarrays,
the second flower adds ( - 2)·1 = - 2, because he is in one of chosen subarrays,
the third flower adds 1·2 = 2, because he is in two of chosen subarrays,
the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,
the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.
Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl’s happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!
Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother.
The second line contains the flowers moods — n integers a1, a2, …, an ( - 100 ≤ ai ≤ 100).
The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], …, a[ri].
Each subarray can encounter more than once.
Output
Print single integer — the maximum possible value added to the Alyona’s happiness.
Examples
input
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
output
7
input
4 3
1 2 3 4
1 3
2 4
1 1
output
16
input
2 2
-1 -2
1 1
1 2
output
0
Note
The first example is the situation described in the statements.
In the second example Alyona should choose all subarrays.
The third example has answer 0 because Alyona can choose none of the subarrays.
题意:给你一个长度为n的区间,和m个子区间。如果选择该子区间,和值加上子区间的和。问和最大为多少。
题解:
如果子区间的和大于0,就选,否则就不选。
代码:
#include <bits/stdc++.h> #define ll long long using namespace std; ll n,m; ll a[150]; ll l,r; int main() { cin>>n>>m; for(int i=1;i<=n;i++) { cin>>a[i]; if(i>1) a[i]+=a[i-1]; } ll ans=0; while(m--) { cin>>l>>r; ans+=max(0ll,a[r]-a[l-1]); } cout<<ans<<endl; }
相关文章推荐
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- cf264 div2E题 E. Caisa and Tree(dfs转化为有根树暴力求解)
- Topcoder SRM 146 Div2 1000(dfs搜索,经典过桥问题,很有意思)
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
- Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- 【 Codeforces Round #268 (Div. 1)】B.Two Set【dfs找增广路】
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和
- Codeforces Round #394 (Div. 2)-E. Dasha and Puzzle(dfs)
- HDU 5423 Rikka with Tree(bfs or dfs)——BestCoder Round #53(div.1 div.2)
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀
- Codeforces Round #321 (Div. 2)(C)模拟,DFS
- odeforces Beta Round #25 (Div. 2 Only), problem: (D) Roads not only in Berland 图论 DFS
- 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons
- 【HDU5635 BestCoder Round 74 (div2)A】【贪心 暴力】LCP Array 相邻后缀最大相同前缀 输出字符串方案数
- Codeforces Round #197 (Div. 2) C. Xenia and Weights(DFS)
- Codeforces Round #345 (Div. 2) D. Image Preview 前缀和