【UOJ 454】打雪仗(通信题)(分块)
打雪仗
题目链接:UOJ 454
题目大意
通信题。
两个人 A 有一个 2n 的 01 的字符串,B 有 n 个要知道的位置。
两个人可以传 01 给对方,然后最多的人不能传超过 4/3n(会多一点)个字符。
要你操作,使得 B 知道每个位置是 0 是 1。
思路
发现大概是 \(2n\) 的 \(\frac{2}{3}\),考虑能不能均摊一下了两个人的消费。
(因为你最暴力可以理解为一个人不说话,第二个把所有人都丢给他,考虑让那个不说话也说说话提供信息)
然后不难想到各种 50 分的写法。
然后再看看这个 \(\frac{2}{3}\),我们考虑分块。
给的那个通过一个一开始的信息得到最多的是哪个,然后整块给,然后剩下的就看对面的 01 串是 \(1\) 就表示要,就给。
那这样剩下两个块至多给 \(\frac{2}{3}n\) 加上你 \(\frac{1}{3}2n\) 刚好 \(\frac{2}{3}2n\)。
然后看看要的那个,那大的块自己挑要的,然后剩下两个就你自己要全给,告诉对面每个位置要不要。
所以就是 \(\frac{2}{3}2n\),那就也没问题。
(麻了不会编译,只会提交调试绷不住了)
代码
Alice
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin;
char get_bit() {
return getchar();
}
void send_bit(char ch) {
putchar(ch);
fflush(stdout);
}
int n, m;
string s;
struct init_t {
init_t() {
fin.open("alice.in");
fin >> n >> m >> s;
}
} init_t;
int L1 = 1, R1 = 666, L2 = 667, R2 = 1332, L3 = 1333, R3 = 2000;
int main() {
// if (get_bit() == '0') {
// for (int x = 0; x < n; ++x) send_bit(s[x]);
// } else {
// for (int x = 0; x < n; ++x) send_bit(s[x + n]);
// }
s = " " + s;
int op = get_bit() - '0' + (get_bit() - '0') * 2;
if (op == 0) {
for (int i = L1; i <= R1; i++) send_bit(s[i]);
for (int i = L2; i <= R2; i++) if (get_bit() == '1') send_bit(s[i]);
for (int i = L3; i <= R3; i++) if (get_bit() == '1') send_bit(s[i]);
}
else if (op == 1) {
for (int i = L1; i <= R1; i++) if (get_bit() == '1') send_bit(s[i]);
for (int i = L2; i <= R2; i++) send_bit(s[i]);
for (int i = L3; i <= R3; i++) if (get_bit() == '1') send_bit(s[i]);
}
else if (op == 2) {
for (int i = L1; i <= R1; i++) if (get_bit() == '1') send_bit(s[i]);
for (int i = L2; i <= R2; i++) if (get_bit() == '1') send_bit(s[i]);
for (int i = L3; i <= R3; i++) send_bit(s[i]);
}
}
Bob
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin;
char get_bit() {
return getchar();
}
void send_bit(char ch) {
putchar(ch);
fflush(stdout);
}
ofstream fout;
void answer(string s) {
fout << s << endl, exit(0);
}
const int N = 1000;
int n, m, pos[N + 1];
struct init_t {
init_t() {
int x;
fin.open("bob.in");
fout.open("bob.out");
fin >> n >> m;
for (x = 1; x <= n; ++x) fin >> pos[x];
}
} init_t;
int L1 = 1, R1 = 666, L2 = 667, R2 = 1332, L3 = 1333, R3 = 2000;
int main() {
// if (pos[n] == n) {
// send_bit('0');
// } else {
// send_bit('1');
// }
// string ans = "";
// for (int x = 0; x < n; ++x) {
// ans += get_bit();
// }
int num0 = 0, num1 = 0, num2 = 0;
for (int i = 1; i <= n; i++) {
if (L1 <= pos[i] && pos[i] <= R1) num0++;
if (L2 <= pos[i] && pos[i] <= R2) num1++;
if (L3 <= pos[i] && pos[i] <= R3) num2++;
}
string s = "";
if (num0 >= num1 && num0 >= num2) {
send_bit('0'); send_bit('0');
int now = 1;
for (int i = L1; i <= R1; i++) {
char c = get_bit();
if (now <= n && pos[now] == i) now++, s += c;
}
for (int i = L2; i <= R2; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
for (int i = L3; i <= R3; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
}
else if (num1 >= num0 && num1 >= num2) {
send_bit('1'); send_bit('0');
int now = 1;
for (int i = L1; i <= R1; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
for (int i = L2; i <= R2; i++) {
char c = get_bit();
if (now <= n && pos[now] == i) now++, s += c;
}
for (int i = L3; i <= R3; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
}
else if (num2 >= num0 && num2 >= num1) {
send_bit('0'); send_bit('1');
int now = 1;
for (int i = L1; i <= R1; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
for (int i = L2; i <= R2; i++)
if (now <= n && pos[now] == i) now++, send_bit('1'), s += get_bit();
else send_bit('0');
for (int i = L3; i <= R3; i++) {
char c = get_bit();
if (now <= n && pos[now] == i) now++, s += c;
}
}
answer(s);
}