首页 > 其他分享 >2024.12.4 周三

2024.12.4 周三

时间:2024-12-05 11:54:56浏览次数:7  
标签:2024.12 int res long nx ny 周三 define

2024.12.4 周三


Q1. 1000

给定01串,操作:选择l,r,将s[r]放到s[l]前:s[l]s[l+1]...s[r-1]s[r]->s[r]s[l]s[l+1]...s[r-1],代价为r-l+1/区间长度。

问最小代价将01串由小到大排序。

Q2. 1300

给定2行'<''>'组成的字符串,起点[1,1],可选4个方向走一步,然后必须根据所在字符走一步。

问是否可到达[2,m]。

Q3. 1300

给定n,m。n个人在排队,你在n+1的位置。操作:你在i,与前面的j交换位置,代价a[j]+b[j+1]+..+b[i-1]。

问最小代价使你排到前m。

Q4. 1400

给定n,k,a数组。i:1~n卡牌数字i的数量为a[i]。你可以购买k张任意数字的卡牌。

问排列后可构成1~n的排列的连续子数组的最大数量。

------------------------独自思考分割线------------------------

  • 这次的4道题算是比较顺,Q2 Q3都是20分钟左右,Q4近一个小时,主要是在对2种答案的讨论上费了时间,最后发现殊途同归。

A1.

两点:1.发现最终必须000011111,必要操作就是将后面的0提到1之前。

2.发现最小代价就是对最近的0操作,操作次数就是前面1的个数。即对每个0:res+=pre_1+1。

A2.

这里发现bfs可做就直接bfs了,当然也有其他巧妙的做法。

A3.

两点:1.发现本质就是在1~m选一个a[i],中间的话选min(a[i],b[i])最优。

2.操作就是m+1n:选min(a[i],b[i]),m1枚举作为终点的位置,倒序枚举可简化当其不为终点时取min(a[i],b[i])。

A4.

4点:1.贪心构造出最多排列的方式:1234123412 且数量取决于最少数量牌的个数

2.二分找出贪心购买后最少数量,枚举一次找到最少数量牌的个数。

3.设最少数量为k,其个数为cnt,,发现答案就是n*(k-1)+1+n-cnt;

4.二分的时候可能爆long long,开int128。(重测了一次,l开1e12确实有问题,r需要开到1e13,long long也没问题)

------------------------代码分割线------------------------

A1.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
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
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    string s;
    cin >> s;
    int pre = 0, res = 0;
    for (auto v : s)
        if (v == '0')
        {
            if (pre)
                res += pre + 1;
        }
        else
            pre++;
    cout << res << endl;
}

A2.

#include <bits/stdc++.h>
#define int long long //
// #define endl '\n'     // 交互/调试 关
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
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n = 2, m;
    cin >> m;
    vector<string> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        a[i] = " " + a[i];
    }
    vector<vector<int>> vis(n + 1, vector<int>(m + 1));
    auto bfs = [&]()
    {
        queue<pair<int, int>> q;
        vis[1][1] = 1;
        q.push({1, 1});
        int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
        while (q.size())
        {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i], ny = y + dy[i];
                if (nx < 1 || nx > 2 || ny < 1 || ny > m || vis[nx][ny])
                    continue;
                vis[nx][ny] = 1;
                // bug2(nx, ny);
                if (a[nx][ny] == '<')
                    ny--;
                else
                    ny++;
                if (nx < 1 || nx > 2 || ny < 1 || ny > m || vis[nx][ny])
                    continue;
                // bug2(nx, ny);
                vis[nx][ny] = 1;
                q.push({nx, ny});
            }
        }
    };
    bfs();
    cout << (vis[n][m] ? "YES" : "NO") << endl;
}

A3.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
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
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1), b(n + 1), pre(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
        pre[i] = pre[i - 1] + b[i];
    }
    int s = 0, cnt = 0;
    for (int i = m + 1; i <= n; i++)
        s += min(a[i], b[i]);

    int res = 1e18;
    int has = 0;
    for (int i = m; i; i--)
    {
        res = min(res, s + a[i]);
        s += min(a[i], b[i]);
    }
    cout << res << endl;
}

A4.

#include <bits/stdc++.h>
// #define int long long //
#define int __int128
#define endl '\n' // 交互/调试 关
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
void _();
int re()
{
    int s = 0, f = 1;
    char ch = getchar();
    while (ch > '9' || ch < '0')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9')
        s = s * 10 + ch - 48, ch = getchar();
    return s * f;
}
void wr(int s)
{
    if (s < 0)
        putchar('-'), s = -s;
    if (s > 9)
        wr(s / 10);
    putchar(s % 10 + 48);
}
void wr(int s, char ch) { wr(s), putchar(ch); }
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    T = re();
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, k;
    n = re(), k = re();
    vector<int> a(n);
    for (int &x : a)
        x = re();
    auto ok = [&](int x)
    {
        int res = 0;
        for (auto v : a)
            if (x > v)
                res += x - v;
        return res <= k;
    };
    int l = 0, r = 1e13;
    while (r - l - 1)
    {
        int mid = l + r >> 1;
        if (ok(mid))
            l = mid;
        else
            r = mid;
    }
    int kk = k;
    int cnt = 0;
    for (auto v : a)
        if (l >= v)
            kk -= l - v, cnt++;
    cnt -= kk;
    // bug2(l, cnt);
    int res = n * (l - 1) + 1 + n - cnt;
    // if (l * n == n + k)
    //     bug(1);
    // res = n * (l - 1) + 1;
    // if (!k)
    //     res = n * l - n - 1;
    wr(res, '\n');
}

标签:2024.12,int,res,long,nx,ny,周三,define
From: https://www.cnblogs.com/jkkk/p/18588214

相关文章

  • 2024.12.4~2024.12.8
    2024.12.4刚回到北京,呃NOIP也过去了,在家也摆烂了一段时间了,也该做出些调整了怎么说呢,NOIP之前做的计划,虽然并没有严格遵守下去,但也是起到了一个推波助澜的效果的并且计划中的一些条目到目前还适用,所以我就不做什么大的删改,主打的就是一个继承约法n章(省选版):1.作息:6:00起床,7:......
  • 2024.12.3 周二
    2024.12.3周二Q1.1100给定两个长度为n和n+1的数组a,b。每次操作:选择a的任意一个数+1/-1/复制到末尾。问将a变成b的最小操作次数。Q2.1200设定一个数组是美丽的:当其可以通过任意次操作将数组里的数变成同一个数字,操作:如果a[i-1]==a[i+1],则可使a[i]=a[i-1]。问删除数组......
  • 2024.12.3
    //计算每个人的平均成绩JavaPairRDD<String,Double>averages=scores.join(counts).mapValues(newFunction<Tuple2<Integer,Integer>,Double>(){@OverridepublicDoublecall(Tuple2<Integer,Integer>tuple){return(double)tu......
  • 2024.12.2 周一
    2024.12.2周一Q1.1100给定一个数字(32位以内),使用1,0,-1构造二进制数位,同时保证不能有相邻的非0数存在。Q2.1200给定2个相同数位的数(<=1e100),任意操作:交换2数中相同位的数字使两数之积最大。Q3.1300前缀后缀板题Q4.1400给定n,m(<=2e6)。a:1n,b:1m,问:满足a+b是b*g......
  • 2024.12.3(周二)
    #导入必要的库fromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.svmimportSVCfromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_score,classification......
  • 2024.12.2(周一)
    importnumpyasnpfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_scorefromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_score,confusion_matrix,make_scorerfromsklearn.treeimpo......
  • 2024.12.6(周五)
    #导入相关库importnumpyasnpfromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_split,cross_val_scorefromsklearn.clusterimportKMeansfromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_scor......
  • 2024.12.5(周四)
    #导入必要的库importnumpyasnpfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.naive_bayesimportGaussianNBfromsklearn.metricsimportaccuracy_score,precision_score,reca......
  • 2024.12.9(周一)
    importnumpyasnpimportpandasaspdfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_split,cross_val_score,StratifiedKFoldfromsklearn.ensembleimportRandomForestClassifierfromsklearn.metricsimportaccuracy_score,prec......
  • 2024.11.27(周三)
    importpandasaspdfromsklearn.datasetsimportload_irisfromsklearn.model_selectionimportcross_val_score,cross_validate,StratifiedKFoldfromsklearn.ensembleimportRandomForestClassifierfromsklearn.metricsimportprecision_score,recall_score,......