Codeforces Round #381 (Div. 2)(A(dfs)+B(前缀和))

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次数要多,过程中记录最小值即可。
代码:

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值