A - First ABC 2
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 1e5 + 10, M = N * 2;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n; cin >> n;
string s; cin >> s;
bool flag = false;
for (int i = 0; i + 2 < s.size(); i ++) {
if (s.substr(i, 3) == "ABC") {
flag = true;
cout << i + 1 << endl;
break;
}
}
if (!flag) cout << -1 << endl;
return 0;
}
B - Prefix and Suffix
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 1e5 + 10, M = N * 2;
string s, t;
bool check1()
{
for (int i = 0; i < s.size(); i ++) {
if (s[i] != t[i]) return false;
}
return true;
}
bool check2()
{
for (int i = s.size() - 1, j = t.size() - 1; i >= 0; i --, j --) {
if (s[i] != t[j]) return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n, m; cin >> n >> m;
cin >> s >> t;
if (check1() && check2()) cout << 0 << endl;
else if (check1() && !check2()) cout << 1 << endl;
else if (!check1() && check2()) cout << 2 << endl;
else cout << 3 << endl;
return 0;
}
C - Festival
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 2e5 + 10, M = N * 2;
int a[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++)
{
int t = lower_bound(a, a + m, i) - a;
cout << a[t] - i << endl;
}
return 0;
}
E - Product Development
从n个物品中选择若干件使得价值最小,01背包问题的模型,我们使用f[a1][a2][a3][a4][a5]表示每个参数分别为a1、a2、a3、a4、a5时的最小代价,类似于背包问题,我们倒序枚举将第一维优化掉。
初始化时将所有的值置为INF,f[0][0][0][0][0] = 0。使用h表示每个位置参数的值应该为多少,这里主要是为了应对k < 5时其他参数为0而不是p的问题。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
#define int long long
typedef long long LL;
const int N = 110, M = N * 2, INF = 1e18;
int g[N][6], c[N];
int f[6][6][6][6][6], h[6]; // 表示参数分别为xi的情况下的最小代价
void init()
{
for (int i = 0; i < 6; i ++)
for (int j = 0; j < 6; j ++)
for (int k = 0; k < 6; k ++)
for (int l = 0; l < 6; l ++)
for (int r = 0; r < 6; r ++)
f[i][j][k][l][r] = INF;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n, k, p; cin >> n >> k >> p;
for (int i = 1; i <= n; i ++)
{
cin >> c[i];
for (int j = 1; j <= k; j ++) {
cin >> g[i][j];
}
}
init();
f[0][0][0][0][0] = 0;
for (int i = 1; i <= k; i ++) h[i] = p;
for (int i = 1; i <= n; i ++)
for (int a1 = h[1]; a1 >= 0; a1 --)
for (int a2 = h[2]; a2 >= 0; a2 --)
for (int a3 = h[3]; a3 >= 0; a3 --)
for (int a4 = h[4]; a4 >= 0; a4 --)
for (int a5 = h[5]; a5 >= 0; a5 --)
{
int x1 = min(h[1], a1 + g[i][1]), x2 = min(h[2], a2 + g[i][2]), x3 = min(h[3], a3 + g[i][3]), x4 = min(h[4], a4 + g[i][4]),
x5 = min(h[5], a5 + g[i][5]);
f[x1][x2][x3][x4][x5] = min(f[x1][x2][x3][x4][x5], f[a1][a2][a3][a4][a5] + c[i]);
}
cout << (f[h[1]][h[2]][h[3]][h[4]][h[5]] == INF ? -1 : f[h[1]][h[2]][h[3]][h[4]][h[5]]) << endl;
return 0;
}