A who ate the cake
代码:
#include <iostream>
using namespace std;
bool st[4];
int main() {
int a, b;
cin >> a >> b;
st[a] = st[b] = true;
if(a == b) cout << "-1";
else {
for(int i = 1; i <= 3; i ++ ) {
if(!st[i]) cout << i;
}
}
return 0;
}
B piano 2
代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
bool st[N];
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n + m);
for(int i = 0; i < n + m; i ++ ) {
cin >> a[i];
if(i < n)
st[a[i]] = true;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for(int i = 1; i < a.size(); i ++ ) {
if(st[a[i - 1]] && st[a[i]]) {
cout << "Yes";
return 0;
}
}
cout << "No";
return 0;
}
C bingo 2
问题:
思路:模拟
代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int x, m;
cin >> x >> m;
vector<int> row(x + 1);
vector<int> col(x + 1);
vector<int> dia1, dia2;
for(int i = 1; i <= m; i ++ ) {
int n;
cin >> n;
n --;
row[n / x] ++;
if(row[n / x] == x) {
cout << i;
return 0;
}
col[n % x] ++;
if(col[n % x] == x) {
cout << i;
return 0;
}
if(n % x == n / x)
dia1.push_back(x);
if(dia1.size() == x) {
cout << i;
return 0;
}
if(n % x + n / x == x - 1)
dia2.push_back(x);
if(dia2.size() == x) {
cout << i;
return 0;
}
}
cout << "-1" << endl;
return 0;
}
D intersecting intervals
问题:
思路:排序+二分
代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 5e5 + 10;
pair<int, int> interval[N];
int n;
int main() {
cin >> n;
for(int i = 1; i <= n; i ++ ) {
int x, y;
cin >> x >> y;
interval[i] = {x, y};
}
sort(interval + 1, interval + n + 1);
long long ans = 0;
for(int i = 1; i <= n - 1; i ++ ) {
int x = interval[i].second;
int l = i + 1, r = n;
while(l < r) {
int mid = l + r + 1 >> 1;
if(interval[mid].first <= x) l = mid;
else r = mid - 1;
}
//if(i == 1) cout << l;
if(x >= interval[l].first) {
ans += l - i;
}
}
cout << ans;
return 0;
}
E guess the sum
问题:
思路:bfs最短路
代码:
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = (1 << 18) + 10;
int way[N];
int n, l, r;
int qmi(int a) {
int res = 1;
int b = 2;
while(a) {
if(a & 1) {
res *= b;
}
a >>= 1;
b *= b;
}
return res;
}
void bfs(int u) {
way[u] = u;
queue<int> q;
q.push(u);
while(q.size()) {
auto t = q.front();
q.pop();
for(int i = 0; i <= 18; i ++ ) {
int x = qmi(i);
for(auto y: {t - x, t + x}) {
if(y < 0 || y > (1 << n)) continue;
if(way[y] == -1) {
way[y] = t;
q.push(y);
}
}
if(t != 0 && t & x) break;
}
}
}
void solve() {
memset(way, -1, sizeof way);
cin >> n >> l >> r;
r ++;
bfs(l);
int ans = 0;
for(int i = r; i != l; i = way[i]) {
int ll = way[i], rr = i;
int t = 1;
if(ll > rr) {
t *= -1;
swap(ll, rr);
}
cout << "? " << __lg(rr - ll) << " " << ll / (rr - ll) << endl;
int res;
cin >> res;
ans = (ans + t * res + 100) % 100;
}
cout << "! " << ans << endl;
}
int main() {
int t;
t = 1;
while(t -- ) {
solve();
}
return 0;
};
F mst
问题:
思路:由于边权很小,因此可以枚举边权,用并查集维护边集,并查集[i]表示的是所有权重小于i的集合。再加边操作时,从小到大枚举权重,如果某时刻出现自环,则说明该自环出现时刻对应的边要被插入的这条边替代,以后的集合也是如此
代码:
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
int p[N][12], _size[N][12], sum[N];
int find(int x, int w) {
if(p[x][w] != x)
p[x][w] = find(p[x][w], w);
return p[x][w];
}
void solve() {
int n, m, ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) {
for(int j = 1; j <= 10; j ++ ) {
p[i][j] = i;
_size[i][j] = 1;
}
}
for(int i = 1; i <= n - 1; i ++ ) {
int a, b, c;
cin >> a >> b >> c;
ans += c;
for(int j = c; j <= 10; j ++ ) {
int pa = find(a, j);
int pb = find(b, j);
p[pa][j] = pb;
_size[pb][j] += _size[pa][j];
sum[j] += c;
}
}
//for(int i = 1; i <= 10; i ++ ) cout << sum[i] << " ";
for(int j = 1; j <= m; j ++ ) {
int a, b, c;
cin >> a >> b >> c;
int pre = 1;
bool flag = false;
for(int i = c; i <= 10; i ++ ) {
int pa = find(a, i);
int pb = find(b, i);
if(!flag) pre = i;
if(pa == pb) {
flag = true;
sum[i] = sum[i] - pre + c;
} else {
p[pa][i] = pb;
_size[pb][i] += _size[pa][i];
sum[i] += c;
}
if(_size[pb][i] == n) ans = min(ans, sum[i]);
}
//for(int i = 1; i <= 10; i ++ ) cout << sum[i] << " ";
cout << ans << endl;
}
}
int main() {
int t;
t = 1;
while(t -- ) {
solve();
}
return 0;
}
G待补