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

AtCoder Beginner Contest 231

时间:2023-04-30 18:12:21浏览次数:44  
标签:AtCoder ch cout Beginner int auto cin read 231

A - Water Pressure

#include<bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin >> n;
	printf("%.6lf\n" , n / 100.0 );
    return 0;
}

B - Election

#include<bits/stdc++.h>

using namespace std;

int main() {
	ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
	int n;
	cin >> n;
	map<string,int> cnt;
	for( string s ; n ; n -- )
		cin >> s , cnt[s] ++;
	int ans = 0;
	string res;
	for( auto [k,v] : cnt )
		if( v > ans ) ans = v , res = k;
	cout << res ;
    return 0;
}

C - Counting 2

#include<bits/stdc++.h>

using namespace std;

int main() {
	ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
	int n , q;
	cin >> n >> q;
	vector<int> a(n);
	for( auto & i : a ) cin >> i;
	sort( a.begin() , a.end() ); 
    for( int x ; q ; q -- ){
    	cin >> x;
    	cout << n - (lower_bound( a.begin() , a.end() , x ) - a.begin()) << "\n";
    }
    return 0;
}

D - Neighbors

要求是一条链,首先每个人最多只能与两个人相邻,并且不能出现成环的情况。

首先统计度数,然后跑一个拓扑序即可。

#include<bits/stdc++.h>

using namespace std;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 1e5+5;
bool vis[N];
vector<int> e[N] ;
int d[N];

int main() {
	int n = read() , m = read() , cnt = 0;
    for(int u , v ; m ; m -- )
        u = read() , v = read() , e[u].push_back(v) , e[v].push_back(u) ,d[v] ++ , d[u] ++;
    queue<int> q;
    
    for( int i = 1 ; i <= n ; i ++ )
        if( e[i].size() > 2 ) cout << "No\n" , exit(0);
        else if( e[i].size() <= 1 ) q.push(i);

    while( !q.empty() ){
        int u = q.front(); 
        cnt ++ , q.pop();
        for( auto v : e[u] ) 
            if( --d[v] == 1 ) q.push(v);
    }
    if( cnt == n ) cout <<"Yes\n";
    else cout << "No\n";

    
    return 0;
}

E - Minimal payments

设\(f[n][x]\)表示前\(n\)种货币表示\(x\)所用的最少数目。

因为\(a_i|a_{i+1}\)恒成立,所以如果要用\(a_n\)就一定要尽可能的多用才是最优解所以$f[n-1][x\mod a_n] + \left \lfloor \frac{x}{a_n} \right \rfloor $

还有一种情况就是找零此时就是\(f[n-1][x-x\mod a_n] + \left \lceil \frac{x}{a_n} \right \rceil\)

两种取最小值即可,这样 dp 的话比较难写,所以可以采用记忆化搜索的形式。

#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N = 60+5;
int a[N];
map<int,int> f[N];

int calc( int n , int x ){
    if( n == 1 ) return x;
    if( f[n].find(x) != f[n].end() ) return f[n][x];
    f[n][x] = calc( n-1 , x % a[n] ) + x / a[n];
    if( x % a[n] ) f[n][x] = min( f[n][x] , calc( n-1 , a[n] - x % a[n] ) + x / a[n] + 1 );
    return f[n][x];
}

int32_t main() {
    int n , x;
    cin >> n >> x;
    for( int i = 1 ; i <= n ; i ++ ) cin >> a[i];
    cout << calc( n , x );
}

F - Jealous Two

给第一个人的礼物是\(x\),第二个人的礼物是\(y\),要求保证\(A_x\ge A_y \and B_y \ge B_x\)

首先我们可以把礼物按照\(A_i\)排序,然后用求逆序对的方式求一下有多少\(y\)满足\(y<x\and By \ge B_x\)

值得注意的是\(A_x=A_y\and B_x=B_y\)的情况下,我们只统计了\(x>y\)的没有统计\(x<y\),解决方法就是用map统计每种物品出现的次数,然后组合数计算一下就好。

注意\(B_i\)范围很大,需要离散化。

#include<bits/stdc++.h>

using namespace std;

#define int long long

typedef pair<int, int> pii;
typedef long long ll;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

struct BinaryIndexedTree {
#define lowbit(x) ( x & -x )
    int n;
    vector<int> b;

    BinaryIndexedTree(int n) : n(n), b(n + 1, 0) {};

    BinaryIndexedTree(vector<int> &c) { // 注意数组下标必须从 1 开始
        n = c.size(), b = c;
        for (int i = 1, fa = i + lowbit(i); i <= n; i++, fa = i + lowbit(i))
            if (fa <= n) b[fa] += b[i];
    }

    void add(int i, int y) {
        for (; i <= n; i += lowbit(i)) b[i] += y;
        return;
    }

    int calc(int i) {
        int sum = 0;
        for (; i; i -= lowbit(i)) sum += b[i];
        return sum;
    }
};


int32_t main() {
    int n = read();
    vector<pii> p(n);
    vector<int> t;
    for (auto &[a, b]: p) a = read();
    for (auto &[a, b]: p) b = read(), t.push_back(b);
    sort(t.begin(), t.end());
    for (auto &[a, b]: p)
        b = lower_bound(t.begin(), t.end(), b) - t.begin() + 1;
    sort(p.begin(),p.end() ,[](pii a , pii b){
        if( a.first != b.first ) return a.first < b.first;
        return a.second > b.second;
    });

    BinaryIndexedTree B(n);
    int res = 0;
    map<pii,int> cnt;
    for( auto it : p ) cnt[it] ++;
    for( auto [k,v] : cnt ) res += v*(v-1)/2;
    for (auto [a, b]: p)
        B.add(b, 1), res += B.calc(n) - B.calc(b-1);
    cout << res;
    return 0;
}

标签:AtCoder,ch,cout,Beginner,int,auto,cin,read,231
From: https://www.cnblogs.com/PHarr/p/17365559.html

相关文章

  • AtCoder比赛记录(一)
    这里记录的是这个账号的比赛情况。ARC1572023-2-25Solved:4/62189->2216C(Medium,1802)给定一个XY矩阵,一条左上角到右下角的路径的分值定义为路径上连续两个Y的组数。求所有可能路径的分值的平方和。Solution:经典DP。递推两个量,一个是到(i,j)所有路径的分值和\(f_{i,j}\),......
  • AtCoder比赛记录(二)
    这里记录的是这个账号的比赛情况。ABC3002023-4-29Solved:7/80->1200F(Medium-,1846)给定由o和x组成的字符串\(S\)。将\(S\)复制\(m\)遍,然后将其中\(k\)个x改成o,求改完之后连续的o最多可能有多少个。Solution:贪心。设最多能改完\(t\)个完整的\(S\),考虑三类情况:最长连续......
  • CS231N assignment 3 _ GAN 学习笔记 & 解析
    这篇文章之所以来的比较早,是因为我们机器人比赛字符识别数据集不够,想自己造点数据集其实课程内容总结所谓GAN,原理很简单,我们有一个生成器网络和鉴别器网络,生成器生成假的数据,鉴别器分辨真假,二者知己知彼互相优化自己,从而达到博弈的效果.实际操作中,我们一般是......
  • AtCoder Regular Contest 116 F Deque Game
    洛谷传送门AtCoder传送门很强的博弈+性质题。下文令A为Takahashi,B为Aoki。发现单独考虑一个序列\(a_1,a_2,...,a_n\):若\(n\bmod2=0\):若A为先手,答案为\(\max(a_{\frac{n}{2}},a_{\frac{n}{2}+1})\);若B为先手,答案为\(\min(a_{\frac{n}{2}},a_{\frac......
  • AtCoder Regular Contest 123 E Training
    洛谷传送门AtCoder传送门不妨假设\(B_X\leB_Y\)。设\(f(x)=A_X+\frac{x}{B_X},g(x)=A_Y+\frac{x}{B_Y},F(x)=\left\lfloor{f(x)}\right\rfloor,G(x)=\left\lfloor{g(x)}\right\rfloor\),题目即求\(\sum\limits_{x=1}^n[F(x)=G(x)]\)。我一开始尝试化简......
  • AtCoder Regular Contest 126 E Infinite Operations
    洛谷传送门AtCoder传送门算是对这篇博客的补充吧。设\(a_1\lea_2\le\cdots\lea_n\)。发现最优操作中一定是对相邻的数进行操作,因为如果\(a_j\)想把\(x\)给\(a_i\)(\(i<j\)),最优是依次操作\((j-1,j,x),(j-2,j-1,x),...,(i,i+1,x)\)。这样\(x\)就能造成\((j-i)......
  • 【AtCoder】Forbidden Pattern
    题目链接分析首先考虑哪些串能被删空。下面只考虑长度为偶数的串。考虑这样一个(错误的)算法:从左往右依次加入串中的字符,然后能删则删。这个算法对于结尾为A的串一定能删空。对称地,开头为B的串也一定能被删空。现在只需要考虑开头为A结尾为B的串。如果它能被删空,则一定存......
  • AtCoder Regular Contest 112 F Die Siedler
    洛谷传送门AtCoder传送门感觉太人类智慧了。设\(A=(c_1,c_2,...,c_n)\)表示当前每种牌的数量,\(f(A)\)为状态\(A\)只进行换牌操作最终最少剩下几张牌。\(f(A)\)是可以贪心求出的,因为策略必然是能换则换。并且我们发现依次换\(2,3,...,n,1\),最后\(c_2\simc_n\)都......
  • AtCoder Regular Contest 123 C 1, 2, 3 - Decomposition
    洛谷传送门AtCoder传送门从低位往高位考虑。设当前个位为\(k\),暴力搜索这一位向上进\(i\)位,设\(\left\lfloor\frac{n}{10}\right\rfloor-i\)的答案为\(t\)。若\(t>10i+k\)显然就不可行,因为就算个位全部填\(1\)也不能补齐;否则\(n\)的答案就是\(\max(t,\l......
  • AtCoder Regular Contest 120 F Wine Thief
    洛谷传送门AtCoder传送门Hint如果是一个环怎么做?Answer由于是一个环,因此环上每个点对最终答案造成的贡献都相同。设$f_{i,j}$为长度为$i$的序列选$j$个不相邻的点的方案数,则$f_{i,j}=\binom{i-j+1}{j}$。应该很好理解,考虑一个长度为$i-j+1$的链,链头、链尾和两......