Codeforces Round 952 (Div. 4)A-H2

A.Creating Words

题目大意:给出长度为3的字符串a和b,要求把a和b的首字母交换后输出a和b

分析:模拟即可

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

void solve()
{
    string a, b;
    cin >> a >> b;
    for (int i = 0; i < a.size(); i ++ )
        if (!i) cout << b[i];
        else cout << a[i];
    cout << ' ';
    for (int i = 0; i < b.size(); i ++ )
        if (!i) cout << a[i];
        else cout << b[i];
    cout << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

B.Maximum Multiple Sum

题目大意:给定n,求一个数x,使得所有的kx<=n的kx总和最大。

分析:很容易就能发现只有当n=3时x要取3,其他情况x取2。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

void solve()
{
    int x;
    cin >> x;
    if (x == 3) cout << 3 << endl;
    else cout << 2 << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

C.Good Prefixes

题目大意:定义一个数组满足其中一个数等于其他数的和即为好数组,只有一个0也为好数组。给出数组a,问有多少个a的前缀是好数组。

分析:由于a的前缀数组个数为n个,所有我们可以枚举每一个数组,然后判断是否为好数组,可以遍历然后维护总和以及最大值,满足总和减去最大等于最大即为好数组。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

void solve()
{
    int n;
    cin >> n;
    ll sum = 0, mx = 0, res = 0;
    for (int i = 0; i < n; i ++ )
    {
        ll x;
        cin >> x;
        sum += x;
        mx = max(x, mx);
        if (mx == sum - mx) res ++ ;
    }
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

D.Manhattan Circle

题目大意:给出一个由‘.’组成的平面,其中有一个点(a,b),然后半径k,满足点到(a,b)的距离小于k的点均变为‘#’,先给出平面,求这个(a, b)点

分析:可以发现我们从上到下从左到右遍历这个平面,第一次遇到‘#’的所在列一定为所求点的所在列,然后我们在向下寻找最后一个’#‘然后得到所在行,然后将这两个行数加起来除二就是所求行。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;
string mp[N];

void solve()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) cin >> mp[i];
    int resx = -1, resy = -1;
    for (int i = 0; i < n && resx == -1; i ++ )
    {
        for (int j = 0; j < m && resx == -1; j ++ )
        {
            if (mp[i][j] == '#')
            {
                resy = j + 1;
                int top = i, un = i;
                for (un = i; un < n; un ++ )
                    if (mp[un][j] != '#')
                    {
                        un -- ;
                        break;
                    }
                resx = (top + un) / 2 + 1;
            }
        }
    }
    cout << resx << ' ' << resy << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

E.Secret Box

题目大意:有一个长宽高为x,y,z的盒子,现在要放一个体积为k的盒子,求所有可能的盒子中的最多放法。

分析:可以发现x,y,z并不大,所以我们可以考虑枚举被放入盒子的长宽,然后高直接求出,再计算可能数,取最大即可。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

void solve()
{
    ll x, y, z, k, res = 0;
    cin >> x >> y >> z >> k;
    for (ll i = 1; i <= x; i ++ )
    {
        if (k % i == 0)
        {
            ll k1 = k / i;
            for (ll j = 1; j <= y; j ++ )
                if (k1 % j == 0 && k1 / j <= z)
                {
                    res = max(res, (x - i + 1) * (y - j + 1) * (z - k1 / j + 1));
                }
        }
    }
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

F.Final Boss

题目大意:有一个boss,有h的血量,你有n个技能,每个技能能打出ai的伤害,需要ci个回合才能冷却完成重新使用,每个技能独立,刚开始所有技能都可以使用,求最小回合使boss血量为0或者更低。

分析:显然技能有了就用可以最快,然后我们可以发现回合数越大总计打出的伤害越高,满足二分性质,所以我们可以二分回合数,然后计算伤害,如果大于等于h则向左,否则向右,计算伤害可以简单计算一下。这里check函数里的sum会爆longlong,加个判断提前出来防止一下,赛时数据没算好也是被hack了。

时间复杂度:O(nlogn)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

ll h, n;
ll a[N], c[N];

bool check(ll x)
{
    ll sum = 0;
    for (int i = 0; i < n; i ++ )
    {
        sum += a[i] * (x / c[i] + 1);
        if (sum >= h) return true;
    }
    return sum >= h;
}

void solve()
{
    cin >> h >> n;
    for (int i = 0; i < n; i ++ ) cin >> a[i];
    for (int i = 0; i < n; i ++ ) cin >> c[i];

    ll l = 0, r = 4e10;
    while (l < r)
    {
        ll mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l + 1 << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

G.D-Function

题目大意:定义D(n)为n的每一位数字之和,求在10^l到10^r之间满足D(k * n) == k * D(n) 的n的个数,对1e9+7取模。

分析:可以发现当某一位数*k后大于等于10,那么就无法满足题意,那么我们可以先求得*k后小于10的数字个数sum,然后可以发现0到10^x(不含)是好求的,为sum^x,所以我们可以得出答案为sum^r - sum^l,阶乘可以快速幂解决。

时间复杂度O(logn)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;
const ll mod = 1e9 + 7;

ll qpow(ll x, ll y)
{
    ll res = 1;
    while (y)
    {
        if (y & 1) res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}

void solve()
{
    ll l, r, k;
    cin >> l >> r >> k;
    ll sum = 0, res = 1;
    for (int i = 0; i < 10; i ++ )
        if (i * k < 10) sum ++ ;
    res = (qpow(sum, r) - qpow(sum, l) + mod) % mod;
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

H1.Maximize the Largest Component (Easy Version)

题目大意:给出一个只有'.'和'#'的二维平面,可以将某一行或者某一列全部变成'#',求'#'相互连通的最大数量。

分析:h1是简单部分,我当时想到的是枚举每一行每一列变成'#'后得到的答案取最大,求答案可以先将已经连通的记录下来,然后走遍行或列,维护当前已经算入的连通集合即可,写h2的时候发现可以优化。

时间复杂度:O(n*m*(logn+logm))

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;

int n, m;
string mp[N];//地图
vector<int> flag[N];//存点i,j属于哪一个集合
int sum[N];//存某个集合中有多少点
int tx[4] = {0, 0, 1, -1};
int ty[4] = {1, -1, 0, 0};

void dfs(int x, int y, int id)//dfs将相邻点归于同一集合
{
    flag[x][y] = id;
    sum[id] ++ ;
    for (int i = 0; i < 4; i ++ )
    {
        int nx = x + tx[i];
        int ny = y + ty[i];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
        if (mp[nx][ny] == '.' || flag[nx][ny]) continue;
        dfs(nx, ny, id);
    }
}

void solve()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )
    {
        cin >> mp[i];
        flag[i].clear();
        for (int j = 0; j < m; j ++ )
            flag[i].push_back(0);
    }

    int idx = 1;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
            if (!flag[i][j] && mp[i][j] == '#')
            {
                sum[idx] = 0;
                dfs(i, j, idx);
                idx ++ ;
            }
    
    int res = 0;
    for (int i = 0; i < n; i ++ )//按行
    {
        set<int> st;//存当前已经添加过的集合
        int nsum = 0;
        for (int j = 0; j < m; j ++ )
        {
            if (mp[i][j] == '#' && st.find(flag[i][j]) == st.end())//如果路上有这个没被添加过集合
            {
                st.insert(flag[i][j]);
                nsum += sum[flag[i][j]];
            }
            else if (mp[i][j] == '.')//如果这个位置是点
            {
                nsum ++ ;
                if (i > 0 && mp[i - 1][j] == '#' && st.find(flag[i - 1][j]) == st.end())
                {
                    st.insert(flag[i - 1][j]);
                    nsum += sum[flag[i - 1][j]];
                }
                if (i < n - 1 && mp[i + 1][j] == '#' && st.find(flag[i + 1][j]) == st.end())
                {
                    st.insert(flag[i + 1][j]);
                    nsum += sum[flag[i + 1][j]];
                }
            }
        }
        res = max(res, nsum);
    }
    for (int i = 0; i < m; i ++ )//按列,然后同上
    {
        set<int> st;
        int nsum = 0;
        for (int j = 0; j < n; j ++ )
        {
            if (mp[j][i] == '#' && st.find(flag[j][i]) == st.end())
            {
                st.insert(flag[j][i]);
                nsum += sum[flag[j][i]];
            }
            else if (mp[j][i] == '.')
            {
                nsum ++ ;
                if (i > 0 && mp[j][i - 1] == '#' && st.find(flag[j][i - 1]) == st.end())
                {
                    st.insert(flag[j][i - 1]);
                    nsum += sum[flag[j][i - 1]];
                }
                if (i < m - 1 && mp[j][i + 1] == '#' && st.find(flag[j][i + 1]) == st.end())
                {
                    st.insert(flag[j][i + 1]);
                    nsum += sum[flag[j][i + 1]];
                }
            }
        }
        res = max(res, nsum);
    }
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

H2.Maximize the Largest Component (Hard Version)

题目大意:相比于H1,改变的是操作,从某一行或某一列改成了某一行和某一列。

分析:这里可以沿用H1的想法,然后发现可以将值存起来,遍历每一行,每一列O(n*m)的,但是问题在于需要解决某些集合被重复计算的问题,然后我发现每个集合的贡献是对于一个行区间和一个列区间的,这两个区间重复的部分就是被重复计算的部分,我们将行和列看作二维平面就是这样的

然后我们可以发现这很像二维数组差分,所以我们可以通过调整二维数组差分来获得当行为i列为j时所能够得到的集合贡献,然后加上这两条线上的'.'点提供的贡献就是可能的所求值,取最大即可。

时间复杂度:O(n*m)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;

int n, m;
string mp[N];
vector<ll> flag[N], s[N];//s既是差分数组也是所需要的
ll sum[N];
int l, r, t, u;//存集合的最边界
ll sumh[N], suml[N];//存每一行和每一列上'.'的数量
int tx[4] = {0, 0, 1, -1};
int ty[4] = {1, -1, 0, 0};

void dfs(int x, int y, int id)
{
    flag[x][y] = id;
    sum[id] ++ ;
    t = min(t, x - 1), u = max(u, x + 1), l = min(l, y - 1), r = max(r, y + 1);
    for (int i = 0; i < 4; i ++ )
    {
        int nx = x + tx[i];
        int ny = y + ty[i];
        if (nx <= 0 || ny <= 0 || nx > n || ny > m) continue;
        if (mp[nx][ny] == '.' || flag[nx][ny]) continue;
        dfs(nx, ny, id);
    }
}

void solve()
{
    cin >> n >> m;
    for (int i = 0; i <= n; i ++ )
    {
        if (i)
        {
            cin >> mp[i];
            mp[i] = " " + mp[i];
        }
        flag[i].clear(), s[i].clear();
        for (int j = 0; j <= m; j ++ )
            flag[i].push_back(0), s[i].push_back(0);
    }

    int idx = 1;
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            if (!flag[i][j] && mp[i][j] == '#')
            {
                sum[idx] = 0;
                l = m + 1, r = 0, t = n + 1, u = 0;
                dfs(i, j, idx);
                s[0][l] += sum[idx];
                s[t][l] -= sum[idx];
                if (r < m) s[0][r + 1] -= sum[idx], s[t][r + 1] += sum[idx];
                s[t][0] += sum[idx];
                if (u < n) s[u + 1][0] -= sum[idx], s[u + 1][l] += sum[idx];
                if (r < m && u < n) s[u + 1][r + 1] -= sum[idx];//差分数组计算,建议画图理解
                idx ++ ;
            }
    //将差分数组通过前缀和改成所需数组
    for (int i = 0; i <= n; i ++ )
        for (int j = 0; j <= m; j ++ )
        {
            if (i) s[i][j] += s[i - 1][j];
            if (j) s[i][j] += s[i][j - 1];
            if (i && j) s[i][j] -= s[i - 1][j - 1];
        }
    //求每一行和每一列上'.'的数量
    for (int i = 1; i <= n; i ++ )
    {
        sumh[i] = 0;
        for (int j = 1; j <= m; j ++ )
        {
            if (mp[i][j] == '.')
                sumh[i] ++ ;
        }
    }
    for (int i = 1; i <= m; i ++ )
    {
        suml[i] = 0;
        for (int j = 1; j <= n; j ++ )
        {
            if (mp[j][i] == '.')
                suml[i] ++ ;
        }
    }

    ll res = 0;
    for (int i = 1; i <= n; i ++ )
    {
        for (int j = 1; j <= m; j ++ )
        {
            ll num = s[i][j] + sumh[i] + suml[j];
            if (mp[i][j] == '.') num -- ;//相加部分如果为'.'则反复计算要去除
            res = max(res, num);
        }
    }
    cout << res << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

这场也是成功ak了啊,虽然有两发不必要的wa,但是总体感觉还是很好的,h2蛮有意思的感觉很锻炼思维。

  • 29
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值