Codeforces Round #160 (Div. 2)

problem set: http://codeforces.com/contest/262

contest tutorial: http://codeforces.com/blog/entry/6398


Problem A

#include <iostream>

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	int ans = 0;
	while (n--) {
		int a, cnt = 0;
		cin >> a;
		while (a != 0) {
			if (a%10 == 4 || a%10 == 7)
				++cnt;
			a/=10;
		}
		if (cnt <= k)
			++ans;
	}
	cout << ans << endl;
	return 0;
}

Problem B

#include <iostream>
#include <climits>
#include <algorithm>

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	int a[100000];
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	int ans = 0;
	int minval = INT_MAX;
	bool kflag = false;
	for (int i= 0; i < n; ++i) {
		if (a[i] < 0 && k > 0) {
			ans += -a[i];
			--k;
		} else {
			ans += a[i];
		}
		minval = min(minval, abs(a[i]));
	}
	if (k > 0 && k%2 == 1)
		ans -= 2 * abs(minval);
	cout << ans << endl;
	return 0;
}

Problem C

#include <iostream>
#include <climits>
#include <algorithm>

using namespace std;

int main() {
	int n, m, q[100000], a[100000];
	int minq = INT_MAX;
	int tmp = 0, cnt;
	int ans = 0;
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> q[i];
		minq = min(minq, q[i]);
	}
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a, a+n);
	cnt = minq;
	for (int i = n - 1; i >= 0; --i) {
		if (tmp > 0) {
			--tmp;
			cnt = minq;
			continue;
		}
		--cnt;
		if (cnt == 0)
			tmp = 2;
		ans += a[i];
	}
	cout << ans << endl;
	return 0;
}

Problem D

dp[j][k]  <==  dp[n][j][k], add one dimension "... out of first i numbers ..."

(Please notice to use int64_t for method numbers)

#include <iostream>
#include <cstdint>

using std::cin;
using std::cout;
using std::endl;

const int MAX = 51;
int64_t dp[MAX][MAX][MAX] = {0}; // dp[i][j][k]: methods no. of j numbers out of first i numbers with sum k

int main() {
	int n, p, a[MAX];
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	cin >> p;
	for (int i = 0; i <= n; ++i)
		dp[i][0][0] = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= i; ++j) {
			for (int k = 1; k <= p; ++k) {
				dp[i][j][k] = dp[i-1][j][k];
				if (k >= a[i])
					dp[i][j][k] += dp[i-1][j-1][k-a[i]];
			}
		}
	}
	double ans = 0;
	for (int l = 1; l <= p; ++l) {
		for (int j = 0; j <= n; ++j) {
			double tmp = 1.0 * dp[n][j][l];
			for (int k = n; k > j; --k) // multiply C(n,j)
				tmp = tmp * (k-j) / k;
			ans += tmp;
		}
	}
	printf("%.10lf\n", ans);
	return 0;
}



Codeforces Round #160 tutorial

By  Sereja7 days agoIn English

262A - Roma and Lucky Numbers

This problem just need to simulate everithing that was given in statment.

262B - Roma and Changing Signs

We will "reverse" numbers from the begining to the end while numebrers are negative and we did't spend all k operations.
In the end there can leave some operetions, and we will "reverse" only one numeber, with minimal value k(that remains) times.

261A - Maxim and Discounts

Ofcourse the most optimal way is to use discount with minimal q_i. We will sort our numbers and will go from the end to begin of the array. We will by use our discount as soon as it will be possible. It's not hard to see that we will buy all the items with numbers I (zero-numeration from the end of the sorted array) such, that I%(q+2)<q.

261B - Maxim and Restaurant
If all people can come, we will return answer as n.
If it is impossible, there will be finded some person that will be the last to come. We will brtueforce this value. Then we will detrminate dp[i,j,s] in how many ways j persons from the first i with total length s can be in the resturant. It is easy to calculate.
Then we will add to the answer values dp[n][i][s]i!(n-1-i)! for all i,s such that s+p[h]>P. Where P — total length of the table, p[h] — length of the fixed person.

261C - Maxim and Matrix
For fixed m, the sum in the last row will be 2^(bit_count(m+1)-1). So now if T is not power of 2, answer is 0. Else we can find number of bits that we need. And know we have stndart problem. How many numbers form 2 to n+1 have exactly P bits in binary presentation of the number. It is well known problem can be done using binomial cooficients. We will count number of numebers smaller then out number with fixed prefix.

261D - Maxim and Increasing Subsequence

This problem can be done using dp[i,j] where we can end our increasing sequence with length i and last number j. Its not hard to understand that number of states will be n*b. To make a tranfer we need to know array first[j] — first position of the number j in the sequence b, next[i][j] — first position of the number j in the sequence b after position i.

Now its easy to calculate all values.

261E - Maxim and Calculator

I will add tutorial later. But I will give you a hint: number of numbers with maximal prime divisor<=100 is near 3000000 numbers.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值