首页 > 其他分享 >AtCoder Beginner Contest 044

AtCoder Beginner Contest 044

时间:2024-08-15 21:50:15浏览次数:6  
标签:pre AtCoder false Beginner int cin long using 044

A - Tak and Hotels (ABC Edit)

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n, k, x, y;
	cin >> n >> k >> x >> y;
	
	int ans = 0;
	if (n <= k) {
		ans += n * x;
	} else {
		ans += k * x + (n - k) * y;
	}
	cout << ans;
	return 0;
}

B - Beautiful Strings

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int st[26];

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	string s;
	cin >> s;
	for (auto i : s) {
		st[i - 'a'] ++;
	}
	for (int i = 0; i < 26; i++) {
		if (st[i] % 2 == 1) {
			cout << "No\n";
			return 0;
		}
	}
	cout << "Yes";
	return 0;
}

C - Tak and Cards

\(\rm f[i][j][k]\) 表示前 \(i\) 个物品中,选择 \(j\) 个物品,价值之和为 \(k\) 的方案数。状态转移方程为:\(dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-x[i]]\) 【确保不要访问到负数下标,下标从1开始

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

long long f[51][51][2600], pre[1005];

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int N, A;
	cin >> N >> A;
	vector<int> x(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> x[i];
		pre[i] = pre[i - 1] + x[i];
	}
	
	for (int i = 0; i <= N; i++) f[i][0][0] = 1;//初始化,啥都不选也是一种方案
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= i; j++) {
			for (int k = 1; k <= pre[i]; k++) {
				if (k >= x[i]) f[i][j][k] += f[i - 1][j - 1][k - x[i]];
				f[i][j][k] += f[i - 1][j][k];
			}
		}
	}
	
	long long ans = 0;
	for (int i = 1; i <= N; i++) ans += f[N][i][A * i];
	cout << ans;
	return 0;
}

其实这就是变形后的 \(01\) 背包问题,可以将第一维优化掉,但需注意改变循环的遍历顺序,从小到大的状态更新方式,若仍然顺序遍历的话,被忽略的 \(i-1\) 维度的 \(j\) 和 \(k\) 会被污染,影响状态转移。因此需要逆向枚举

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

long long f[51][2600], pre[1005];

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int N, A;
	cin >> N >> A;
	vector<int> x(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> x[i];
		pre[i] = pre[i - 1] + x[i];
	}
	
	f[0][0] = 1;
	for (int i = 1; i <= N; i++) {
		for (int j = i; j >= 1; j--) {
			for (int k = pre[i]; k >= x[i]; k--) {
				f[j][k] += f[j - 1][k - x[i]];
			}
		}
	}

	long long ans = 0;
	for (int i = 1; i <= N; i++) ans += f[i][A * i];
	cout << ans;
	return 0;
}

标签:pre,AtCoder,false,Beginner,int,cin,long,using,044
From: https://www.cnblogs.com/pangyou3s/p/18361856

相关文章

  • Solution - Atcoder ARC171D Rolling Hash
    对于这个\(\operatorname{hash}(A_L,\cdots,A_R)\),一个比较经典的想法是做差分,即令\(s_i=\sum\limits_{j=1}^iA_jB^{i-j}\)。于是\(\operatorname{hash}(A_L,\cdots,A_R)=s_R-s_{L-1}\timesB^{R-L+1}\not=0\)。那么也就是\(s_R\not=s_{L-1}\ti......
  • Solution - Atcoder ABC155F Perils in Parallel
    首先可以按\(a_i\)排序,对于区间\([l_i,r_i]\)可以通过二分确定实际影响到的\(b_i\)的区间。考虑到区间\(i\in[l,r]\)的\(b_i\)都取反影响到的元素过多,考虑去减少影响到的元素。于是可以想到令\(c_i=b_i\oplusb_{i-1}\),那么对于区间\([l,r]\)操作实际影响......
  • Atcoder nomura2020F Sorting Game
    首先考虑如果固定了\(a\),如何判定这个\(a\)是否能被排序。如果存在\(a_i>a_j(i<j)\),那么\(a_i\)肯定要交换到\(a_j\)后面,那么就肯定会交换\(a_i,a_j\)。于是合法条件就是如果存在\(a_i>a_j(i<j)\),那么\(a_i,a_j\)只相差一个二进制位。那就还能知道此时一......
  • AtCoder Regular Contest 041 D 辺彩色
    洛谷传送门AtCoder传送门比较有意思的小清新题。第一步是时光倒流,看成是每次经过一条未被访问过的边才染色。奇偶相关容易想到二分图。发现若有一个黑白交替的奇环(即从一个点开始遍历完整个环得到的颜色序列是黑白交替地),那我们可以先染完这个环。又因为它是奇环,所以我们遍历......
  • AtCoder ABC 366题解
    前言本文部分思路来自于网络,仅供参考。A-Election2题目大意给定两个市长候选人的票数,求结果是否已经确定。解题思路如果剩下的人全部投票给票少的人票少的人也不能赢,则结果就已经确定了。code#include<bits/stdc++.h>usingnamespacestd;intmain(){intn,t,......
  • AI Python for Beginners-Andrew吴恩达-study notes(2)
    1Introduction    itisbelievedthatwiththehelpofAIchatbotwecanlearnpythonmoreeasilyanditwillbeamazingtoautomatetasksusingPython2 CompletingatasklistwithAI2.1List①listisasinglevariableoftype thatholdsm......
  • AtCoder Beginner Contest 366
    A-Election2(abc366A)题目大意\(n\)张票,目前投了\(t\)给高桥,\(a\)给青木。问剩余票随便分配,是否都是一个结局。解题思路考虑最好情况,即剩下票全部投给当前票少的,看看能不能超过对方,会则结局会变,否则不会变。神奇的代码#include<bits/stdc++.h>usingnamespaces......
  • AtCoder Beginner Contest 366 C,D题解
    C-BallsandBagQuery题解题意没什么好说的,给出q次查询,进行求解思路很简单的一道题,但这篇题解的作用是引出unordered_set,这个东西的作用类似set,但没有排序,相当于哈希。unordered_set有几种操作,接下来介绍三种insert,没什么可说的,普通的插入erase,进行弹出size,返回大......
  • G - AtCoder Office
    G-AtCoderOfficeProblemStatement$N$peopleworkattheAtCoderoffice.Theofficekeepsrecordsofentriesandexits,andtherehavebeen$M$entriesandexitssincetherecordsbegan.The$i$-th$(1\leqi\leqM)$recordisrepresentedbyapairof......
  • AtCoder Regular Contest 100 F Colorful Sequences
    洛谷传送门AtCoder传送门比较有趣的一个题。考虑一个弱化版,算colorful序列个数。有一个\(O(nK)\)的dp,大概就是设\(f_{i,j}\)为考虑到第\(i\)个数,当前最长互不相同后缀长度为\(j\)。转移考虑若往后面填一个在这\(j\)个数以外的数就能使\(j\getsj+1\),因此\(......