2022.5.22
AtCoder Beginner Contest 252
A - ASCII code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int main()
{
int n;
scanf("%d", &n);
printf("%c", n);
return 0;
}
B - Takahashi's Failure
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int a[N], b[N];
int vis[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n;i++)
{
cin >> a[i];
}
for (int i = 1; i <= k;i++)
{
cin >> b[i];
vis[a[b[i]]] = 1;
}
sort(a + 1, a + 1 + n);
int f = 0;
for (int i = n; i >= 1; i--)
{
if(a[i]==a[n])
{
if(vis[a[i]])
f = 1;
}
else
break;
}
if(f)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
C - Slot Strategy
枚举每个数字出现的位置以及出现相同的次数位置,对于每个数字而言最大的操作次数为j + 10 * (vis[i][j]-1)),我们需要取0-9之间的最小值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int vis[15][105];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n;i++)
{
string s;
cin>>s;
for (int j = 0; j < s.length();j++)
{
int pos = s[j] - '0';
vis[pos][j]++;
}
}
int ans = INF;
for (int i = 0; i <= 9;i++)
{
int res = 0;
for (int j = 0; j < 10; j++)
{
res = max(res, j + 10 * (vis[i][j]-1));
}
ans = min(ans, res);
}
cout << ans << '\n';
return 0;
}
D - Distinct Trio
这样的三元组等价于,i<j<k,a[i]<a[j]<a[k]的个数,我们记录a[i]出现的次数,做一遍前缀和,最后累加个数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e9;
int a[N], sum[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for (int i = 1; i <= n;i++)
{
cin >> a[i];
sum[a[i]]++;
}
for (int i = 1;i<=2e5;i++)
{
sum[i] += sum[i - 1];
}
ll ans = 0;
for (int i = 1; i <= n;i++)
{
ans += 1ll * (sum[a[i] - 1]) * 1ll * (n - sum[a[i]]);
}
cout << ans;
return 0;
E - Road Reduction
跑一遍djikstra,满足d[2]+d[3]...+d[n]最小的则是遍历2-n的点,满足d[u]=d[v]+w的边v
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
struct Edge
{
int to, w, id;
};
vector<Edge> p[N];
int dis[N];
bool vis[N];
void dijkstra()
{
memset(vis, 0, sizeof vis);
memset(dis, 0x3f, sizeof dis);
priority_queue<pii, vector<pii>, greater<pii>> que;
que.push({0,1});
dis[1] = 0;
while(!que.empty())
{
int t = que.top().second;
que.pop();
if(vis[t])
continue;
vis[t] = 1;
for(auto tt:p[t])
{
int to = tt.to;
int w = tt.w;
if(dis[to]>dis[t]+w)
{
dis[to] = dis[t] + w;
que.push({dis[to], to});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
for (int i = 1; i <= m;i++)
{
int a, b, c;
cin >> a >> b >> c;
p[a].push_back({b, c, i});
p[b].push_back({a, c, i});
}
dijkstra();
for (int i = 2; i <= n;i++)
{
for(auto t:p[i])
{
int to = t.to;
int w = t.w;
if(dis[i]==dis[to]+w)
{
cout << t.id << ' ';
}
}
}
return 0;
}
F - Bread
合并果子,只是略有不同,有剩余,判断如果有剩余的话就加入小根堆中。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n, l;
cin >> n >> l;
ll sum = 0,ans = 0;
priority_queue<ll, vector<ll>, greater<ll>> que;
for (int i = 1; i <= n;i++)
{
ll x;
cin >> x;
que.push(x);
sum += x;
}
ll res = l - sum;
if(res)
que.push(res);
while(que.size()>1)
{
ll a = que.top();
que.pop();
ll b = que.top();
que.pop();
ll c = a + b;
ans += c;
que.push(c);
}
cout << ans;
return 0;
}