首页 > 其他分享 >AtCoder Beginner Contest 141

AtCoder Beginner Contest 141

时间:2023-03-21 23:56:31浏览次数:55  
标签:AtCoder const Beginner 141 int long -- num ans

AtCoder Beginner Contest 141

D - Powerful Discount Tickets

贪心 + 堆

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 1e5 + 5;
int a[N], n, m, f[N]; //f[i]:用了几张
ll ans;

int main () {
    cin >> n >> m;
    //用几张
    for (int i = 1; i <= n; i++)    cin >> a[i], ans += a[i];
    priority_queue<int> q;
    for (int i = 1; i <= n; i++) {
        int t = a[i];
        while (t > 0)   q.push (t - t / 2), t /= 2;
    }

    while (!q.empty () && m --) {
        //cout << q.top () << endl;
        ans -= q.top ();
        q.pop ();
    }
    cout << ans;

}

//dp

E - Who Says a Pun?

字符串中找到两个最长的一样的不重叠子串
最最最简单的dp

#include <bits/stdc++.h>
#define vi vector<int>

using namespace std;
const int N = 5e3 + 5;
int n, f[N][N], ans; //f[i][j]: i结尾与j结尾的匹配的最大长度
string s;

int main () {
    cin >> n >> s;
    s = ' ' + s;
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (s[i] == s[j]) {
                if (i + f[i-1][j-1] < j)    f[i][j] = f[i-1][j-1] + 1;
                ans = max (ans, f[i][j]);
            }
        }
    }
    cout << ans;
}


//dp

F - Xor Sum 3

不会写,没想到是线性基。
洛谷题解:https://www.luogu.com.cn/problem/solution/AT_abc141_f

啊啊又是位运算 1ll<< 这里!!太可恶了!!

#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 1e5 + 5;
int a[N], cnt[63];
int n, ans;

struct linear_basis {
    int num[63];
    bool insert (int x) {
        for (int i = 60; i >= 0; i--) {
            if ((x >> i & 1) == 0)  continue;
            if (num[i] == 0) {
                num[i] = x;
                return true;
            }
            else    x ^= num[i];
        }
        return false;
    }

    int query () {
        int x = 0;
        for (int i = 62; i >= 0; i --) {
            x = max (x, x ^ num[i]);
        }
        return x;
    }
}T;

signed main () {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        for (int j = 60; j >= 0; j--)    cnt[j] += (a[i] >> j & 1);   
    }

    for (int j = 60; j >= 0; j--) {
        if (cnt[j] % 2 == 0)    continue; 
        ans += (1ll << j); //奇数贡献一直在
        for (int i = 1; i <= n; i++) {
            if (a[i] >> j & 1) {
                a[i] ^= (1ll << j);
            }
        }
    }

    for (int i = 1; i <= n; i++)    T.insert (a[i]);
    cout << ans + 2 * T.query () << endl;

}

//划分成两部分,使得每部分的异或和最大
//恰好符合线性鸡定义
//找到最大ai
//按位考虑:当前位1次数为奇数->直接计入,偶数->x1^x2=0,x1取max,则x2取max

标签:AtCoder,const,Beginner,141,int,long,--,num,ans
From: https://www.cnblogs.com/CTing/p/17242081.html

相关文章

  • [AtCoder] B - Counting Grids
      Thekeyobservationisthatthereisalwaysatmost1cellthatviolatesbothconditions. Proof: ifxviolatesbothconditions,thatmeansallothe......
  • AtCoder Beginner Contest 294
    题解报告基本的一些理解和问题都在注释中A:Filter//水题#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>usingnamespacestd;intm......
  • IC5141和617——ASSURA配置
    前言在系统中同时安装了IC5141和IC617,但是ASSURA的版本号不同,对于两个软件不能通用。在分别安装后,通过自定义控制台命令,切换环境变量ASSURAHOME的值,从而达到对两个版本的......
  • [??记录] AtCoder 练习
    3.19arc066_c(dp,观察)观察:只会在负号右边添加\((/)\)两个位置之间至多一个括号。括号不会嵌套多层。\(f[i][j]\)表示处理完\(i\)个数,有\(j\)个未匹配左括号......
  • AtCoder Beginner Contest 294
    A-Filter(abc294a)题目大意给定一个数组,不改变原顺序,输出是偶数的数。解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL......
  • AtCoder Beginner Contest 293
    A-SwapOddandEven#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ strings; cin>>s; for(inti=0;i+1<s.size();i+=2) swap(......
  • AtCoder Beginner Contest 293
    上周因为GDKOI咕咕咕了A-SwapOddandEven(abc293a)题目大意给定一个字符串,交换每两个相邻字母,输出结果。解题思路模拟即可。神奇的代码#include<bits/std......
  • AtCoder Beginner Contest 293(C,D ,E,F)
    AtCoderBeginnerContest293(C,D,E,F)CC这道题其实还蛮好写的,就是一个\(dfs\),然后我看错了题意,就记录一下这道题的大意是我们需要从\((1,1)\)走到\((n,m)\),我们只......
  • [AtCoder Beginner Contest 281][G. Farthest City]
    和CF1657E的做法十分相似题目链接:G-FarthestCity题目大意:问有多少个\(n(3\len\le500)\)个点的无向连通图满足,若设\(1\)到\(i\)的最短路距离为\(dis_i\),则......
  • AtCoder Regular Contest 158
    Preface这场比赛刚好周日晚上没事就打了,堪堪混过三道题,也算是小上了一波分吧但是由于A题脑抽了一波卡了30min,导致排名不高,也没时间看DE了,属实有点可惜A-+3+5+7显......