2025.2.8——1400

2025.2.8——1400


A 1400

B 1400

------------------------------------------------

  • 思维+贪心。


A

  1. 入手点是考虑每行/列之和为多少。

B

  1. 较难的贪心。
  2. 入手点是转化问题分析方式:分割为两个数组理解为按顺序向两个空数组添加。

------------------------代码------------------------

A

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n + 1, vector<int>(m + 1));
    string order;
    cin >> order;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    auto rowsum = [&](int i)
    {
        int ans = 0;
        for (int j = 1; j <= m; j++)
            ans += a[i][j];
        return ans;
    };
    auto colsum = [&](int j)
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += a[i][j];
        return ans;
    };

    int sx = 1, sy = 1;
    for (auto s : order)
    {
        if (s == 'D')
            a[sx][sy] = -rowsum(sx);
        else
            a[sx][sy] = -colsum(sy);
        if (s == 'D')
            sx++;
        else
            sy++;
    }
    a[sx][sy] = -rowsum(n);
    // bug2(sx, sy);

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << a[i][j] << ' ';
        el;
    }
    // for (int i = 1; i <= n; i++)
    //     bug(rowsum(i));
    // for (int j = 1; j <= m; j++)
    //     bug(colsum(j));
}
// void _()
// {
//     int n, m;
//     cin >> n >> m;
//     vector<vector<int>> a(n + 1, vector<int>(m + 1)), f(n + 1, vector<int>(m + 1)); // 1 2
//     string order;
//     cin >> order;

//     for (int i = 1; i <= n; i++)
//         for (int j = 1; j <= m; j++)
//             cin >> a[i][j];

//     auto rowsum = [&](int i)
//     {
//         int ans = 0;
//         for (int j = 1; j <= m; j++)
//             ans += a[i][j];
//         return ans;
//     };
//     auto colsum = [&](int j)
//     {
//         int ans = 0;
//         for (int i = 1; i <= n; i++)
//             ans += a[i][j];
//         return ans;
//     };

//     int sx = 1, sy = 1;
//     f[sx][sy] = 1;
//     int sum = order[0] == 'D' ? rowsum(1) : colsum(1);
//     for (auto s : order)
//     {
//         if (s == 'D')
//             sx++;
//         else
//             sy++;
//         f[sx][sy] = 1;
//     }

//     for (int i = 1; i <= n; i++)
//         for (int j = 1; j <= m; j++)
//             if (f[i][j])
//             {
//                 if ((j - 1 > 0 && f[i][j - 1]) || (j + 1 <= m && f[i][j + 1]))
//                     f[i][j] = 2;
//             }

//     for (int i = 1; i <= n; i++)
//         for (int j = 1; j <= m; j++)
//             if (f[i][j] == 1)
//                 a[i][j] = sum - rowsum(i);
//     for (int i = 1; i <= n; i++)
//         for (int j = 1; j <= m; j++)
//             if (f[i][j] == 2)
//                 a[i][j] = sum - colsum(j);

//     for (int i = 1; i <= n; i++)
//     {
//         for (int j = 1; j <= m; j++)
//             cout << a[i][j] << ' ';
//         el;
//     }
//     for (int i = 1; i <= n; i++)
//         bug(rowsum(i));
//     for (int j = 1; j <= m; j++)
//         bug(colsum(j));
// }

B

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    int x = 1e12, y = 1e12, res = 0;
    for (int i = 0; i < n; i++)
    {
        if (x > y)
            swap(x, y);
        int a;
        cin >> a;
        if (a <= x)
            x = a;
        else if (a > y)
            x = a, res++;
        else
            y = a;
    }
    cout << res;
    el;
}
// void _()
// {
//     int n;
//     cin >> n;
//     vector<int> s, t;
//     for (int i = 0; i < n; i++)
//     {
//         int x;
//         cin >> x;
//         if (!i)
//             s.push_back(x);
//         else
//         {
//             if (x > s.back())
//                 t.push_back(x);
//             else
//                 s.push_back(x);
//         }
//     }
//     int res = 0;
//     for (int i = 1; i < s.size(); i++)
//         res += s[i] > s[i - 1];
//     for (int i = 1; i < s.size(); i++)
//         res += s[i] > s[i - 1];
//     for (int i = 1; i < t.size(); i++)
//         res += t[i] > t[i - 1];
//     cout << res;
//     el;
// }

posted @   Jkke  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示