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

AtCoder Beginner Contest 317

时间:2023-09-01 17:14:18浏览次数:63  
标签:AtCoder Beginner int 317 cin long vis vector ans

A - Potions

#include <bits/stdc++.h>

using namespace std;

#define int long long

int power(int x, int y, int p) {
    x %= p;
    int ans = 1;
    while (y) {
        if (y & 1) ans = ans * x % p;
        y >>= 1, x = x * x % p;
    }
    return ans;
}

void solve() {
    int a, m;
    cin >> a >> m;
    for (int i = 0; i < 50; i++) {
        if (power(a, i, m) == i % m) cout << i << "," << i;
    }
    cout << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n , h , t;
    cin >> n >> h >> t;
    for( int i = 1 , x ; i <= n ; i ++ ){
        cin >> x;
        if( x + h >= t ) cout << i , exit(0);
    }
    return 0;
}

B - MissingNo.

#include <bits/stdc++.h>

using namespace std;

#define int long long

int power(int x, int y, int p) {
    x %= p;
    int ans = 1;
    while (y) {
        if (y & 1) ans = ans * x % p;
        y >>= 1, x = x * x % p;
    }
    return ans;
}

void solve() {
    int a, m;
    cin >> a >> m;
    for (int i = 0; i < 50; i++) {
        if (power(a, i, m) == i % m) cout << i << "," << i;
    }
    cout << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    for( auto & i : a ) cin >> i;
    sort( a.begin() , a.end() ) ;
    for( int i = 1 ; i < n ; i ++ ){
        if( a[i] != a[i-1] + 1 ) cout << a[i-1] +1 , exit(0);
    }
    return 0;
}

C - Remembering the Days

#include <bits/stdc++.h>

using namespace std;

#define int long long


int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, m, res = 0;
    cin >> n >> m;
    vector<vector<pair<int, int>>> e(n + 1);
    vector<int> vis(n + 1);
    for (int i = 1, x, y, z; i <= m; i++)
        cin >> x >> y >> z, e[x].emplace_back(y, z), e[y].emplace_back(x, z);
    auto dfs = [ &vis , e , &res ]( auto && self , int x , int d ) -> void{
        vis[x] = 1 , res = max( res , d );
        for( auto [y,w] : e[x] ){
            if( vis[y] ) continue;
            self( self , y , d + w );
        }
        vis[x] = 0;
    };

    for( int i = 1 ; i <= n ; i ++ )
        dfs( dfs , i , 0 );
    cout << res << "\n";
    return 0;
}

D - President

注意到\(\sum Z \le 10^5\),这样我们就可以背包了。\(f[i][j]\)表示前\(i\)个区赢得\(j\)张选票的最小花费。

从第\(i\)个选区赢得\(Z_i\)张选票代价是\(w_i=\max( 0 , \left \lceil \frac{X_i+Y_i}{2} \right \rceil -X_i)\)

剩下的就是 01背包

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, sum = 0;
    cin >> n;
    vector<int> a(n + 1), b(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i] >> b[i] >> c[i], sum += c[i];
    vector<vector<int>> f(n + 1, vector<int>(sum + 1, 1e18));
    f[0][0] = 0;
    for (int i = 1, w; i <= n; i++) {
        w = max(0ll, (a[i] + b[i] + 1) / 2 - a[i]);
        for (int j = 0; j <= sum; j++){
            f[i][j] = f[i-1][j];
            if( j >= c[i] ) f[i][j] = min( f[i][j] , f[i-1][j-c[i]] + w);
        }
    }
    int res = 1e18;
    for( int i = (sum+1) / 2 ; i <= sum ; i ++ )
        res = min( res , f[n][i] );
    cout << res << "\n";
    return 0;
}

E - Avoid Eye Contact

赛时想复杂了,直接暴力的标记不能到达的点即可。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const array<int, 4> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, m, sx, sy, ex, ey;
    cin >> n >> m;
    vector<vector<char>> e(n + 1, vector<char>(m + 1));
    vector<vector<bool>> g(n + 1, vector<bool>(m + 1, false));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> e[i][j];


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (e[i][j] == '#') g[i][j] = true;
            else if (e[i][j] == 'S') sx = i, sy = j;
            else if (e[i][j] == 'G') ex = i, ey = j;
            else if (e[i][j] == '>') {
                g[i][j] = true;
                for (int nj = j + 1; nj <= m and e[i][nj] == '.'; nj++)
                    g[i][nj] = true;
            } else if (e[i][j] == '<') {
                g[i][j] = true;
                for (int nj = j - 1; nj >= 1 and e[i][nj] == '.'; nj--)
                    g[i][nj] = true;
            } else if (e[i][j] == '^') {
                g[i][j] = true;
                for (int ni = i - 1; ni >= 1 and e[ni][j] == '.'; ni--)
                    g[ni][j] = true;
            } else if (e[i][j] == 'v') {
                g[i][j] = true;
                for (int ni = i + 1; ni <= n and e[ni][j] == '.'; ni++)
                    g[ni][j] = true;
            }
        }
    }
    vector<vector<int>> dis(n + 1, vector<int>(m + 1, INT_MAX));
    dis[sx][sy] = 0;
    queue<pair<int, int>> q;
    q.emplace(sx, sy);
    vector<vector<bool>> vis(n + 1, vector<bool>(m + 1));
    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();
        if (vis[x][y]) continue;
        vis[x][y] = true;
        for (int i = 0, fx, fy; i < 4; i++) {
            fx = x + dx[i], fy = y + dy[i];
            if (fx < 1 or fy < 1 or fx > n or fy > n) continue;
            if (vis[fx][fy] or g[fx][fy] or dis[fx][fy] <= dis[x][y] + 1) continue;
            dis[fx][fy] = dis[x][y] + 1;
            q.emplace(fx, fy);
        }
    }
    if( dis[ex][ey] == INT_MAX ) cout << "-1\n";
    else cout << dis[ex][ey] << "\n";
    return 0;
}

标签:AtCoder,Beginner,int,317,cin,long,vis,vector,ans
From: https://www.cnblogs.com/PHarr/p/17672410.html

相关文章

  • AtCoder Beginner Contest 292 E - Transitivity
    E-Transitivity原题链接题意:对于一个有向图,进行加边操作,使最终任意的个点具有传递效果,即若a到b有边,b到c有边,那么a到c也要有边,问最少需要进行多少次操作,使得每个节点所能到达的地方都有直接的边,也就是最短距离为1思路:怎么加边才是最优的,举个例子a->b->c->d->e对于a点到其他......
  • AtCoder Beginner Contest 292 D - Unicyclic Components
    D-UnicyclicComponents原题链接题意:判断一个连通块的边和点个数是否相同思路:对它使用并查集吧点击查看代码#include<bits/stdc++.h>usingnamespacestd;constintN=200010,M=N<<1;//维护连通图中点和边的个数intsd[N],se[N],p[N];boolf[N];//谁是祖宗int......
  • Atcoder Beginner Contest 317 解题报告
    AtcoderBeginnerContest317ABC316咋没了。暂时A~E。HintsD$\quad$可以算出每次选举需要的改票数。然后变成了一个经典问题。E$\quad$有点naive。不用担心暴力扫T掉,时间复杂度是真的。F$\quad$F1$\qquadn$这么大一维都枚举不了……诶,$a_i$只有$10$?$\qua......
  • AtCoder Beginner Contest 317 F - Nim
    数位DP#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;intdp[64][10][10][10][2][2][2][2][2][2];intmain(){lln;intb1,b2,b3;cin>>n>>b1>>b2>>b3;memset(dp,-1,sizeofdp);strings......
  • ABC317F题解
    让人头大的数位DP。建议评蓝。个人认为不适合放ABC的F。将三个数二进制拆分,使三个数异或为0相当于每个二进制位三个数中有0或2个是1。所以考虑数位DP,设\(dp[i][m1][m2][m3][lim1][lim2][lim3]\)为第\(i\)位,三个数模\(a\),\(b\),\(c\)分别是\(m1\),\(m2\),\(m3\)......
  • [ABC317G] Rearranging 题解
    取自我的洛谷博客:https://www.luogu.com.cn/blog/SunnyYuan/solution-at-abc317-g借鉴了官方题解思路。思路首先我们要建立一个二分图。对于输入的\(a_{i,j}\),我们可以连接左侧的\(i\)和右侧的\(a_{i,j}\)。比如样例\(1\):注意:左边的\(1,2,3\)和右边的\(1,2......
  • AtCoder Beginner Contest 215
    [ABC215F]DistMax2 二分出min(|xi-xj|,|yi-yj|),双指针维护是否存在满足条件的点对(i,j),假如二分当前值是x,那么|xi-xj|>=x&&|yi-yj|>=x #include<bits/stdc++.h>usingnamespacestd;#defineendl"\n"typedeflonglongll;consti......
  • AtCoder Beginner Contest 314
    A-3.14#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongint32_tmain(){ios::sync_with_stdio(0),cin.tie(0);strings="141592653589793238462643383279502884197169399375105820974944592307816406286208998628034......
  • AtCoder Beginner Contest 315
    A-tcdr#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){strings;cin>>s;for(autoi:s){if(i!='a'andi!='e'andi!='i'andi!='o'andi!='u�......
  • AtCoder Beginner Contest 287 - C (图论简单题)
    目录C-PathGraph?C-PathGraph?题意判断给定的无向简单图是不是一条链思路n个顶点m条边的无向图若为一条链,那么边数\(m=n-1\),n个顶点相互可达,任意一个顶点的度不超过2利用并查集判整体连通性,在建图时统计度数,最后判断即可由此,n个顶点,n-1条边的无向连通......