AtCoder Beginner Contest 137
AtCoder Beginner Contest 137
昨天还是前天写的,忘了,今天补一下
https://atcoder.jp/contests/abc137
A - +-x
#include <bits/stdc++.h>
using namespace std;
int main () {
int a, b;
cin >> a >> b;
cout << max ({a + b, a - b, a * b});
}
B - One Clue
#include <bits/stdc++.h>
using namespace std;
int main () {
int a, b;
cin >> a >> b;
for (int i = max(-1000000, b - a + 1); i <= min (1000000, b + a - 1); i++) cout << i << ' ';
}
C - Green Bin
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
int n;
cin >> n;
map<string, int> mp;
while (n --) {
string s;
cin >> s;
sort (s.begin (), s.end ());
mp[s] ++;
}
ll ans = 0;
for (auto i : mp) {
n = i.second;
ans += 1ll * n * (n - 1) / 2;
}
cout << ans;
}
//hash
D - Summer Vacation
贪心,堆维护
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int, int> pii;
ll ans;
int main () {
int n, m;
cin >> n >> m;
vector <pii> v;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
v.push_back ({a, b});
}
sort (v.begin (), v.end ());
priority_queue <int> q;
for (int i = 1, j = 0; i <= m; i++) {
for (; j < n; j++) {
if (v[j].first > i) break;
q.push (v[j].second);
}
if (!q.empty ()) ans += q.top (), q.pop ();
}
cout << ans << endl;
}
E - Coins Respawn
好题
图论。spfa判正环(可达n才算),求最长路。
正着spfa求最长路,建反向边判正环可达性
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2505, M = 5005;
int n, m, p;
int h[N], ne[M], e[M], w[M], idx;
int d[N], dis[N], cnt[N];
bool vis[N], vis1[N];
vector <int> v[N];
void add (int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
bool spfa () {
queue <int> q;
memset (dis, -0x3f, sizeof dis);
vis[1] = true;
//cnt[1] = 1;
dis[1] = 0;
q.push(1);
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if (!vis1[v]) continue;
int ww = dis[u] + w[i];
if(dis[v] < ww) {
dis[v] = ww;
if(!vis[v]) {
vis[v] = true;
cnt[v] ++;
if(cnt[v] > n) return true;
q.push(v);
}
}
}
}
return false;
}
void dfs (int a) {
if (vis1[a]) return ;
vis1[a] = true;
for (auto b : v[a]) dfs (b);
}
signed main () {
memset (h, -1, sizeof h);
cin >> n >> m >> p;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
add (a, b, c - p);
v[b].push_back (a); //建反向边
}
//有正环则无解
dfs (n);
if (spfa ()) cout << -1;
else cout << max(0ll, dis[n]) << endl;
}
F - Polynomial Construction
一个数论,暂时没懂