封面图源 【动漫作业本】:space.bilibili.com/488779255
[题目地址] codeforces.com/gym/105184
K. Welcome
I. Subnet
A. Update
C. Goose Goose Duck
K. Welcome(签到)
void solve() {
printf("HBCPC2024\n");
}
I. Subnet(签到/模拟)
将ip转为01数组,进行比较
const int maxn = 34;
int n, m;
int b[maxn], b2[maxn];
//char s[34];
string s, s2;
void to_binary2(string s, int *b, int pos) {
int val = atoi(s.c_str());
for (int i = 0; i < 8; ++i) {
b[pos+7-i] = val % 2;
val /= 2;
}
}
void to_binary(string s, int *b) {
int pos = 0, i = 0, nxt = 0;
while (s.find('.', pos) != string::npos) {
nxt = s.find('.', pos);
string sub = s.substr(pos, nxt - pos);
to_binary2(sub, b, i);
pos = nxt + 1;
i += 8;
}
string sub = s.substr(pos);
to_binary2(sub, b, i);
}
void solve() {
cin >> s;
int pos = s.find('/');
to_binary(s.substr(0, pos), b);
int len = atoi(s.substr(pos + 1).c_str());
cin >> n;
while (n--) {
cin >> s2;
to_binary(s2, b2);
bool ok = 1;
for (int i = 0; i < len; ++i) {
if (b[i] != b2[i]) {
ok = 0;
break;
}
}
cout << (ok ? "YES" : "NO") << '\n';
}
}
A. Update(签到)
const int maxn = 100010;
int n, m;
char s[maxn];
//string s, s2;
int mp[28];
void solve() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < n; ++i) {
char &c = s[i];
if (c != 'i') {
++mp[c-'a'];
}
}
m = 26;
int res = 0;
for (int i = 0; i < m; ++i) {
if (mp[i]) {
++res;
}
}
printf("%d\n", res);
}
C. Goose Goose Duck(贪心/优先队列)
排序+维护有序堆/单调栈/单调队列,这种套路还挺常见的
首先,对于所有不小于当前游戏人数的li,我们都给它扔进优队
其次,在优队里边,优先取ri小的(因为更容易“过期”)
const int maxn = 1000010;
int n, m;
struct node {
int l, r, id;
node() {}
node(int _l, int _r, int _id): l(_l), r(_r), id(_id){}
bool operator < (const node &y) const {
return r > y.r;
}
}a[maxn];
bool cmp(const node &a, const node &b) {
return a.l < b.l;
}
void solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].l, &a[i].r);
a[i].id = i + 1;
}
sort(a, a + n, cmp);
priority_queue<node> q;
m = 0;
vector<int> res;
int i = 0;
while (1) {
while (i < n && a[i].l <= m) {
q.push(a[i]);
++i;
}
if (q.empty()) {
break;
}
node cur = q.top();
q.pop();
if (cur.r >= m) {
++m;
res.push_back(cur.id);
}
}
printf("%d\n", m);
for (auto x: res) {
printf("%d ", x);
}
printf("\n");
}
点赞支持,更新其他题目