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 47, 744, 4 are lucky and 5, 17, 467 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.
The first line contains two integers n, k (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.
In a single line print a single integer — the answer to the problem.
3 4 1 2 4
3
3 2 447 44 77
2
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;
}
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.
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.
In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.
3 2 -1 -1 1
3
3 1 -1 -1 1
1
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;
}
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.
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.
In a single line print a single integer — the answer to the problem.
1 2 4 50 50 100 100
200
2 2 3 5 50 50 50 50 50
150
1 1 7 1 1 1 1 1 1 1
3
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;
}