Codeforces Round #160 (Div. 2)——A,B,C

A. Roma and Lucky Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.

Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Roma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.

Input

The first line contains two integers nk (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has.

The numbers in the lines are separated by single spaces.

Output

In a single line print a single integer — the answer to the problem.

Sample test(s)
input
3 4
1 2 4
output
3
input
3 2
447 44 77
output
2
Note

In the first sample all numbers contain at most four lucky digits, so the answer is 3.

In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2.

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    int n,k,num=0;
    scanf("%d%d",&n,&k);
    while(n--)
    {
        int cnt=0,x;
        scanf("%d",&x);
        while(x)
        {
            int c=x%10;
            if(c==4||c==7) cnt++;//找4或7的个数
            x/=10;
        }
        if(cnt<=k) num++;
    }
    printf("%d\n",num);
    return 0;
}

B. Roma and Changing Signs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Roma works in a company that sells TVs. Now he has to prepare a report for the last year.

Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.

The operation of changing a number's sign is the operation of multiplying this number by -1.

Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made.

The second line contains a non-decreasing sequence, consisting of n integers ai (|ai| ≤ 104).

The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.

Output

In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.

Sample test(s)
input
3 2
-1 -1 1
output
3
input
3 1
-1 -1 1
output
1
Note

In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.

In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#define maxn 100005
using namespace std;
int a[maxn];
int main()
{
    int n,k,i;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
        if(a[i]>=0) break;//由于已经排好序,直接数出负数的个数
    int cnt=i;
    int sum=0;
    if(cnt>=k)//如果负数个数大于等于k,优先把排序在前面的变成正的(绝对值较大)
    {
        for(i=0;i<k;i++) a[i]*=-1;
    }
    else//如果负数个数小于k,把所有数全变为正数
    {
        k-=cnt;
        for(i=0;i<cnt;i++) a[i]*=-1;//原本小于0的部分变成正的,再排序
        sort(a,a+n);
        if(k%2) a[0]*=-1;//考虑k的奇偶,如果k为偶数,全正,k为奇数,把最小的数变为负
    }
    for(i=0;i<n;i++) sum+=a[i];
    printf("%d\n",sum);
    return 0;
}

C. Maxim and Discounts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.

There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.

Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.

Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.

Input

The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).

The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.

The numbers in the lines are separated by single spaces.

Output

In a single line print a single integer — the answer to the problem.

Sample test(s)
input
1
2
4
50 50 100 100
output
200
input
2
2 3
5
50 50 50 50 50
output
150
input
1
1
7
1 1 1 1 1 1 1
output
3
Note

In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.

In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define maxn 100007
using namespace std;

int q[maxn];
int a[maxn];
int main()
{
    int n,m,i;
    scanf("%d",&m);
    for(i=0;i<m;i++)
        scanf("%d",&q[i]);
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    //对a,q两个数组从小到大排序,简单的贪心思想:选出q中最小的,每次优惠选最大的(2);
    sort(q,q+m);
    sort(a,a+n);
    int cnt=0,sum=0;
    for(i=n-1;i>=0;)//i--写在此处会有遗漏元素
        if(cnt==q[0])
        {
            i-=2;
            cnt=0;
        }
        else {sum+=a[i];cnt++;i--;}
    printf("%d\n",sum);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值