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

AtCoder Beginner Contest 165

时间:2023-08-01 09:46:35浏览次数:44  
标签:AtCoder Beginner int namespace dfs fa ans 165 include

AtCoder Beginner Contest 165

https://atcoder.jp/contests/abc165

C - Many Requirements

dfs

#include <bits/stdc++.h>

using namespace std;
const int N = 15, M = 55;
int n, m, q, ans;
int a[M], b[M], c[M], d[M], A[N];

void dfs (int x) {
    if (x > n) {
        int sum = 0;
        for (int i = 1; i <= q; i++) {
            if (A[b[i]] - A[a[i]] == c[i])  sum += d[i];
        }
        ans = max (ans, sum);
        return ;
    }

    for (int i = A[x - 1]; i <= m; i++) {
        A[x] = i;
        dfs (x + 1);
        A[x] = 0;
    }
}

int main () {
    cin >> n >> m >> q;
    for (int i = 1; i <= q; i++)    cin >> a[i] >> b[i] >> c[i] >> d[i];
    A[0] = 1;
    dfs (1);
    cout << ans << endl;
}

D - Floor Function

推导:

#include<bits/stdc++.h>

using namespace std;

int main () {
    long long a, b, n;
    cin >> a >> b >> n;
    //for (int i = 1; i <= n; i++)    cout << (a * i) / b - a * (i / b) << ' ';
    n = min (b - 1, n);
    cout << (a * n) / b - a * (n / b);
}

E - Rotation Matching

这题有点看不懂题解。。。

#include<bits/stdc++.h>

using namespace std;

int main () {
    int n, m;
    cin >> n >> m;
    int l = 1, r = m + 1;
    for (int i = 0; i < m; i++) {
        if (l >= r) l = m + 2, r = 2 * m + 1;
        cout << l << ' ' << r << endl;
        l++, r--;
    }
}

F - LIS on Tree

二分求LIS + 树上dfs 然后回溯

(基础是导弹拦截那题)

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5, M = N * 2;
int a[N], ans[N], f[N], n; //f[i]: 长度为i的LIS的结尾最大值
int h[N], e[M], ne[M], idx;

void add (int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs (int u, int fa) {
    int l = 1, r = ans[fa], lst;
    if (fa == 0)     ans[1] = 1, f[1] = a[u];
    else {
        while (l <= r) {
            int mid = l + r >> 1;
            if (a[u] <= f[mid])     r = mid - 1;
            else    l = mid + 1;
        }
        //cout << l << ' ' << r << endl;
        lst = f[l], f[l] = a[u], ans[u] = ans[fa];
        if (l > ans[fa])    ans[u]++;
    }
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa)    continue;
        dfs (j, u);
    }
    f[l] = lst; //回溯
}

int main () {
    memset (h, -1, sizeof h);
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        add (a, b), add (b, a);
    }
    dfs (1, 0);
    for (int i = 1; i <= n; i++)    cout << ans[i] << endl;
}

标签:AtCoder,Beginner,int,namespace,dfs,fa,ans,165,include
From: https://www.cnblogs.com/CTing/p/17595595.html

相关文章

  • AtCoder Beginner Contest 312
    A-Chord#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);strings;cin>>s;if(s=="ACE")cout<<"Yes\n";e......
  • [UNIQUE VISION Programming Contest 2023 Summer(AtCoder Beginner Contest 312) - A
    UNIQUEVISIONProgrammingContest2023Summer(AtCoderBeginnerContest312)-AtCoderA-Chord(atcoder.jp)#include<bits/stdc++.h>#defineendl'\n'usingnamespacestd;intmain(){vector<string>str{"ACE",&qu......
  • Atcoder-Beginner-Contest-312 A~Ex
    \(Atcoder-Beginner-Contest-312\)AB过于简单,在此略去。\(C-Invisible\)\(Hand\)题意:给定长为\(n\)的数组\(a\),长为\(m\)的数组\(b\),找到最小的非负整数\(x\),使得\(\sum_{i=1}^n[a_i\lex]\ge\sum_{i=1}^m[b_i\gex]\)题解:容易发现,随着\(x\)的增大,右式单调不升,左......
  • (AtCoder Beginner Contest 312)
    (AtCoderBeginnerContest312)A-Chord#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string>PSS;constintN......
  • Atcoder ABC259H Yet Another Path Counting
    首先可以想到有组合数的方法:令起点为\((x1,y1)\),终点为\((x2,y2)\),则路径方案数就为\(\binom{x2+y2-x1-y1}{x2-x1}\),这样设有\(k\)个相同颜色的点,时间复杂度就为\(O(k^2)\)。再考虑到还有\(\text{DP}\)方法:令\(f_{x,y}\)为走到\((x,y)\)的方案数,不限制......
  • Atcoder ARC060D Digit Sum
    看到\(n\le10^{11}\),考虑按根号分为两部分处理。对于\(b\le\sqrt{n}\),考虑直接暴力算\(\operatorname{f}(b,n)\)判断是否等于\(s\),这部分的计算量是\(O(\sqrt{n})\)级别的。对于\(\sqrt{n}<b\len\),则这个时候在\(b\)进制下\(n\)也只有两位,考虑列出\(n,s\)......
  • AtCoder Beginner Contest 311
    AtCoderBeginnerContest311FirstABC思路:找到第一个a,b,c都出现的位置#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string&......
  • AtCoder Beginner Contest 311
    A-FirstABC#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongint32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);intn;strings;cin>>n>>s;set<char>c......
  • Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311)
    ToyotaProgrammingContest2023#4(AtCoderBeginnerContest311)A-FirstABC(atcoder.jp)记录一下\(ABC\)是否都出现过了然后输出下标#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;signedmain(){ios::sync_with_stdio(false);cin.tie(n......
  • Atcoder ARC058E Iroha and Haiku
    题目中的式子转化一下即存在一位\(i\)使得到\(i\)时的后缀和存在\(X+Y+Z,Y+Z,Z\),再发现\(X+Y+Z\le17\),考虑状压。设\(f_{i,j}\)为填了\(i\)个数当前后缀和中存在的数的状态为\(j\)(只存\(0\simX+Y+Z\)的数是否存在)的方案数。考虑转移,则下一位可......