cf966-div3

A

题意: 给一个整数 n n n,问 n n n是否为 1 0 x ( x ≥ 2 ) 10^x(x \geq 2) 10x(x2)这种形式。

题解: 垃圾题意,毁我青春。直接先判断前两个数字是否为 10 10 10,如果是,那就判断后边的情况。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

void solve()
{
    string s;
    cin >> s;
    if(s[0] == '1' and s[1] == '0' and s.size() > 2)
    {
        string t = s.substr(2);
        if(t.size() and stoi(t) >= 2)
        {
            if((t.size() == 2 and stoi(t) < 10) or (t.size() == 3 and stoi(t) < 100))
                cout << "No" << endl;
            else
                cout << "Yes" << endl;
        }
        else 
            cout << "No" << endl;
    }
    else
        cout << "No" << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}

B

题意: 有n个位置,分别为[1, n], 第一个人可以随意坐,之后的人要坐的位置,周边必须有一个人以上,问是否可以遵守规则。

题解: 直接开一个数组,记录哪个位置有人坐,然后直接模拟即可,注意边界。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

void solve()
{
    int n;
    cin >> n;
    vector<int> st(n + 1);
    bool is_suc = 1;
    for(int i = 0; i < n; i ++)
    {
        int x;
        cin >> x;
        if(!i)
            st[x] = 1;
        else
        {
            if(x + 1 <= n and st[x + 1])
                st[x] = 1;
            else if(x - 1 >= 1 and st[x - 1])
                st[x] = 1;
            else
                is_suc = 0;
        }
    }
    if(is_suc)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}

C

题意: 有一个长度为 n n n的数组,和 m m m个字符串。判断这 m m m个字符串是否符合规则。规则如下:

  • 如果 a i = a j a_i = a_j ai=aj,那么 s i = s j ( i ≠ j ) s_i = s_j(i \not = j) si=sj(i=j)
  • 如果 s i = s j s_i = s_j si=sj,那么 a i = a j ( i ≠ j ) a_i = a_j(i \not = j) ai=aj(i=j)

题解: 考虑记录一下,比如第一个数字是 3 3 3,第一个字符是 a a a,那么就让 3 3 3 a a a绑定,之后如果出现 a a a,判断是否为 3 3 3,出现 3 3 3,判断是否为 a a a即可。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

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

    int m;
    cin >> m;
    while(m --)
    {
        string s;
        cin >> s;
        if(s.size() != n) //长度不同直接No
        {
            cout << "No" << endl;
            continue;
        }
        map<char, int> mp1;
        map<int, char> mp2;
        //mp1让a = 3, mp2让3 = a
        //python使用字典
        bool is_suc = 1;
        for(int i = 0; i < n; i ++)
        {
            if(mp1.count(s[i])) //如果s[i]在mp1中已经出现了
            {
                if(mp1[s[i]] != a[i])
                {
                    is_suc = 0;
                    break;
                }
            }
            else if(mp2.count(a[i]))
            {
                if(mp2[a[i]] != s[i])
                {
                    is_suc = 0;
                    break;
                }
            }
            else
                mp1[s[i]] = a[i], mp2[a[i]] = s[i];
        }
        if(is_suc)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}

D

题意: 一个长度为 n n n的数组,一个长度为 n n n的字符串。字符串仅包括 ′ L ′ , ′ R ′ 'L', 'R' L,R两种字符,每对 ′ L ′ , ′ R ′ 'L', 'R' L,R都可以得到 [ L , R ] [L, R] [L,R]之间的所有值,如果 [ L , R ] [L, R] [L,R]之间存在点也可以获得,得到完权值之后,将 [ L , R ] [L, R] [L,R]变为 ′ . ′ '.' .,问最大的分数。

题解: 从贪心的角度考虑,如果一个线段被另一个线段完全覆盖,那么先算出小线段的权值,在算大线段的权值一定更优,因为小线段会被计算两次。于是我们考虑,先选取中间的部分,在逐渐选取更大的区间,但是我们在操作时难以判断L的起点,但是我们可以从另一个角度选择,从外向内。但是每次选取之后仍然需要遍历数组,会超时,所以使用前缀和优化。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<ll> s(n + 1);
    for(int i = 1; i <= n; i ++)
        cin >> a[i], s[i] = s[i - 1] + a[i];
    string str;
    cin >> str;
    str = " " + str;

    ll ans = 0;
    int l = 1, r = n;
    while(l < r)
    {
        while(l <= n and str[l] != 'L')
            l ++;
        while(r >= 1 and str[r] != 'R')
            r --;
        //找到最左边的L和最右边的R
        if(r > l)
            ans += s[r] - s[l - 1]; //前缀和计算答案
        l ++, r --;
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}

E

题意: 有一个 n ∗ m n * m nm的大矩形,枚举所有 k ∗ k k * k kk的矩形,每个小矩形的权值为当前矩形中每个点的权值之和。给出 w w w个点值,问如何分配可以让最终权值最大。

题解: 考虑二维差分,二位差分之后可以算出每一个点被使用了几次,使用越多的点,越应该放一个点权大的,把每一个点使用的次数放到一个大根堆 q 1 q1 q1中,再把所有的点权放到另一个大根堆 q 2 q2 q2中。每次取出 q 1 q1 q1 q 2 q2 q2的堆顶,相乘求和即可。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<ll>> s(n + 1, vector<ll>(m + 1, 0));
    for(int i = 1; i + k - 1 <= n; i ++)
        for(int j = 1; j + k - 1 <= m; j ++)
        {
            int sx = i, sy = j;
            int ex = i + k, ey = j + k;
            s[sx][sy] ++;
            s[sx][ey] --;
            s[ex][sy] --;
            s[ex][ey] ++;
            //差分
        }
    priority_queue<ll> q1;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++)
        {
            s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
            q1.push(s[i][j]);
        }
    //前缀和
    int p;
    cin >> p;
    priority_queue<ll> q2;
    while(p --)
    {
        ll x;
        cin >> x;
        q2.push(x);
    }

    ll ans = 0;
    while(q1.size() and q2.size())
    {
        auto t1 = q1.top();
        auto t2 = q2.top();
        q1.pop();
        q2.pop();

        ans += t1 * t2;
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}

F

题意: n n n个矩形,每个矩形的某一行或者某一列被涂满,那么就增加一点价值,每次操作可以涂一个格子,问最少需要多少次操作可以得到 k k k点价值。

题解: 对于每个矩形,优先涂短的那边。然后有 n n n个矩形,可以发现是一个分组背包。 d p i dp_i dpi为以 i i i为价值的最少操作次数。

#include<bits/stdc++.h>
using namespace std;

#define fir first
#define sec second
#define endl "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, pii> PdI;
const int N = 1e5 + 10, M = N * 2, mod = 1e9 + 7, inf = 0x3f3f3f3f, P = 131;

void solve()
{
    int n, k;
    cin >> n >> k;
    vector<int> dp(k + 1, 1e9);
    dp[0] = 0;
    for(int i = 0; i < n; i ++)
    {
        auto ndp = dp;
        //从上一个矩形的状态进行转移。
        int x, y;
        cin >> x >> y;
        for(int j = 0; j <= k; j ++)
            ndp[min(j + x + y, k)] = min(ndp[min(j + x + y, k)], dp[j] + x * y);
        //如果将当前矩形,完全取完的价值。
        //j是枚举先前可能出现的价值,和k取min。
        int s = 0, cnt = 0;
        while(x > 1 or y > 1)
        {
            if(x > y)
                swap(x, y);
            cnt ++;
            s += x;
            y --;

            for(int j = 0; j <= k; j ++)
                ndp[min(j + cnt, k)] = min(ndp[min(j + cnt, k)], dp[j] + s);
            //对于每次操作,j是先前的价值,每次都会用短边进行操作。
        }
        dp.swap(ndp);
        //将当前ndp的数据复制到dp中。
    }
    if(dp[k] > inf / 2)
        cout << -1 << endl;
    else
        cout << dp[k] << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --)
        solve();

    return 0;
}
  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值