https://ac.nowcoder.com/acm/contest/91355
A
#include<bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
vector<int> a(101);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a[x]++;
}
for (int i = 1; i <= k; i++) {
if (a[i] == 0) {
cout << "NO" << endl;
goto end;
}
}
cout << "YES" << endl;
end:;
}
return 0;
}
B
#include<bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin >> T;
cout << fixed << setprecision(15);
while (T--) {
double a;
cin >> a;
cout << (a + sqrtl(a * a + 4)) / 2 << '\n';
}
return 0;
}
C
将数组排序 如若和大于sum则从后往前修改,否则从前往后修改模拟即可
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n, sum;
cin >> n >> sum;
int sum0 = 0;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i], sum0 += a[i];
int ans = 0;
if (sum0 == sum)cout << 0 << endl;
else if (sum0 > sum) {//把大数边小数
sort(a.begin() + 1, a.end());
for (int i = n; i >=1; i--) {
if (sum0 - a[i] - 10000 > sum) {
ans++;
sum0 -= a[i] + 10000;
} else {
ans++;
break;
}
}
cout << ans << endl;
} else {
std::sort(a.begin() + 1, a.end());
for (int i = 1; i <=n; i++) {
if (sum0 - a[i] + 10000 < sum) {
ans++;
sum0 += 10000 - a[i];
} else {
ans++;
break;
}
}
cout << ans << endl;
}
}
return 0;
}
D
分层图 思考方式类dp 考虑使用,dp[i][j]表示走到i,j天没有休息的最短时间。
在路径转移中有两种操作
休息与不休息两种方式
不休息时需满足j+1<=k转移到j+1层
休息时满足转移至第0层
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 200000;
int dist[N + 1][11];
int st[N + 1][11];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i];
vector<vector<int>> g(n + 1);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= k; j++)
{
dist[i][j] = 1e18;
st[i][j] = 0;
}
}
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
if (k != 0)
{
dist[1][1] = 1;
q.push({1, 1, 1});
}
dist[1][0] = a[1];
q.push({a[1], 1, 0});
while (q.size())
{
auto [w, i, j] = q.top();
q.pop();
if (st[i][j])continue;
st[i][j] = 1;
for (auto ne: g[i])
{
//休息
if (dist[ne][0] > a[ne] + w)
{
dist[ne][0] = a[ne] + w;
q.push({dist[ne][0], ne, 0});
}
//不休息
if (j + 1 <= k && dist[ne][j + 1] > w + 1)
{
dist[ne][j + 1] = w + 1;
q.push({dist[ne][j + 1], ne, j + 1});
}
}
}
int ans = 1e18;
for (int i = 0; i <= k; i++)ans = min(ans, dist[n][i]);
cout << ans << endl;
}
return 0;
}