UOJ Easy Round #8 T1 打雪仗 题解

题目链接:

【UER #8】打雪仗

第一次做通信题,写篇\(blog\)加深印象。



首先分析题目,根据数据,最坏情况下\(m\approx \frac23n\)

刚开始时想着把进制压到更高进制输出,不过实现不来放弃了。

那么把\(2n\)分成一些长度为\(3\)的区间,对于\(1,2\)个字符,直接由小\(B\)告诉小\(A\)是否需要,如果需要则小\(A\)发送字符。

对于第\(3\)个字符,无论需不需要都由小\(A\)发出。

那么显然小\(B\)的输出长度正好为\(\frac23n\)。

对于小\(A\),最坏情况下没有一个有用的字符在第\(3\)个位置,则总长度为\(n+\frac13n=\frac43n\)

那么就可以愉快的通过此题了。

时间复杂度 \(O(n)\)

关于实现:



\(Alice.cpp:\)

#include <string>
#include <fstream>
#include <iostream>

int n,m;
std::string s;

int main()
{
    std::ifstream("alice.in")>>n>>m>>s;
    //文件指针用不来,反正输入量不大(x
    for(int i=0;i<=1995;i+=3)
    {
        if(std::cin.get()==49)std::cout<<s[i];
        if(std::cin.get()==49)std::cout<<s[i+1];
        //对应前2个字符,需要才输出。
        (std::cout<<s[i+2]).flush();
        //对于第三个字符,必须输出。
        //同时注意在输出之后立即清空缓冲区,让$Bob$可以接收。
    }
    (std::cout<<s[1998]<<s[1999]).flush();//最后2个单独处理,直接输出,影响不大。
    return 0;
}


\(Bob.cpp:\)

#include <string>
#include <fstream>
#include <iostream>

int n,m;
bool v[2005];
std::string s;

int main()
{
    std::ifstream Fin("bob.in");
    Fin>>n>>m;
    for(int i=1,x;i<=1000;++i)
        Fin>>x,v[x-1]=true;
    for(int i=0;i<=1995;i+=3)
    {
        (std::cout<<v[i]<<v[i+1]).flush();
        //是否需要前2个字符
        if(v[i])s+=std::cin.get();
        if(v[i+1])s+=std::cin.get();
        char c=std::cin.get();
        if(v[i+2])s+=c;
        //第三个字符必须输入,防止影响后面。
    }
    if(v[1998])s+=std::cin.get();
    if(v[1999])s+=std::cin.get();
    //加上最后两个字符
    std::ofstream Fout("bob.out");
    Fout<<s<<'\n';
    //这里可以不flush,因为最后return 0了,自动清空缓存。
    return 0;
}

原文地址:https://www.cnblogs.com/LanrTabe/p/10163699.html

时间: 2024-08-30 15:10:15

UOJ Easy Round #8 T1 打雪仗 题解的相关文章

【UOJ Easy Round #1】

数论/Trie/并查集 猜数 这题我是这样分析的…… $a*b=g*l=n=k^2 \ and \ (g|a,g|b) \Rightarrow (g*a^')*(g*b^')=g*l=k^2 \Rightarrow a^' * b^' =\frac{l}{g}={\frac{k}{g}}^2 \Rightarrow min(a^'+b^')=2* \sqrt{\frac{l}{g}} \ \ max(a^'+b^')=1+\frac{l}{g}$ 然而算ans的时候还需要再乘g……我给忘了 1

Codeforces Round #617 (Div. 3) 题解

目录 Codeforces Round #617 (Div. 3) 题解 前言 A. Array with Odd Sum 题意 做法 程序 B. Food Buying 题意 做法 程序 C. Yet Another Walking Robot 题意 做法 程序 D. Fight with Monsters 题意 做法 程序 E1. String Coloring (easy version) 题意 做法 程序 E2. String Coloring (hard version) 题意 做法

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F 1296A - Array with Odd Sum 思路: 如果一开始数组的sum和是奇数,那么直接YES, 否则:如果存在一个奇数和一个偶数,答案为YES,否则为NO 代码: int n; int a[maxn]; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen(

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

Codeforces Round #534 (Div. 2)题解

Codeforces Round #534 (Div. 2)题解 A. Splitting into digits 题目大意 将一个数字分成几部分,几部分求和既是原数,问如何分可以使得分出来的各个数之间的差值尽可能小 解题思路 将n分成n个1相加即可 AC代码 #include<cstring> #include<string> #include<iostream> #include<cstdio> using namespace std; int main

Codeforces Round #561 (Div. 2) 题解

Codeforces Round #561 (Div. 2) 题解 题目链接 A. Silent Classroom 水题. Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; int n; char s[N], t[N]; int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf(&q