atcoder beginner contest 234题解

atcoder abc234 a-f

A.Weired Function
简单的计算,记得开long long

#include<iostream>
#include<cmath>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x;
    cin>>x;
    int x1 = pow(x,2) + 2 * x + 3;
    //cout<<x1<<"|||\n";
    int x2 = x1 + x;
    //cout<<x2<<"|||\n";
    int x3 = pow(x2,2) + 2 * x2 + 3;
    //cout<<x3<<"|||\n";
    int x4 = pow(x1,2) + 2 * x1 + 3;
    //cout<<x4<<"|||\n";
    int x5 = x3 + x4;
    //cout<<x5<<"|||\n";
    int ans = pow(x5,2) + 2 * x5 + 3;
    cout<<ans;
}

B.Longest Segment
O ( n 2 ) O(n^2) O(n2) 寻找最大直线距离

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
vector<pair<int,int>> v;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++ ){
        int x,y;
        scanf("%d %d",&x,&y);
        v.push_back({x,y});
    }
    double maxx = 0;
    for(int i = 0;i < v.size();i ++ ){
        for(int j = 0;j < v.size();j ++ ){
            maxx = max(maxx,sqrt(abs(v[i].second - v[j].second) * abs(v[i].second - v[j].second) + abs(v[i].first - v[j].first) * abs(v[i].first - v[j].first)));
    }
    }
    printf("%.10lf",maxx);
}

C.Happy New Year
寻找一串由0和2构成的数字中第n个数
本来是在oeis上找到的规律,但是,t了
后来发现位运算就可以解决

#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x;
    cin>>x;
    string ans;
    while(x){
        if(x & 1) ans += '2';
        else ans += '0';
        x >>= 1;
    }
    reverse(ans.begin(),ans.end());
    cout<<ans;
}

D - Prefix K-th Max
寻找一串数字中第n大的数
大根堆的运用

#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#include<queue>
//#define int long long
using namespace std;
const int N = 5e5 + 10;
int a[N];
priority_queue<int,vector<int>,greater<int>> q;
int  main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i = 1;i <= n;i ++ ){
        //int x;
        cin>>a[i];
        if(i <= k){
            q.push(a[i]);
        }
    }
    cout<<q.top()<<"\n";
    for(int i = k + 1;i <= n;i ++ ){
        if(a[i] > q.top()){
            q.pop();
            q.push(a[i]);
        }
        if(q.size() >= k) cout<<q.top()<<"\n";
    }
}

E - Arithmetic Number
寻找最小的大于n的数,并且该数的各个数位组成的数组是等差数列
从-9到9枚举公差,将最小的符合条件的数输出

#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#define int long long
using namespace std;
const int N = 20;
int a[N];
int b[N];
int cnt,ans;
int n;
bool check(int n){
    int d = a[2] - a[1];
    for(int i = 1;i < n;i ++ ){
        if(a[i + 1] - a[i] != d) return 0;
    }
    return 1;
}
bool check1(int x){
    int cnt = 0;
    memset(b,0,sizeof b);
    while(x){
        b[++ cnt] = x % 10;
        x /= 10;
    }
    int d = b[2] - b[1];
    for(int i = 1;i < cnt;i ++ ){
        if(b[i + 1] - b[i] != d) return 0;
    }
    return 1;
}
void check(int x,int d){
    //cout<<x<<"|||\n";
    int t = x;
    for(int i = 2;i <= cnt;i ++ ){
        x -= d;
        if(x < 0 || x > 9) return;
        t = t * 10 + x;
    }
    if(check1(t) && t >= n){
        //cout<<t<<"|||\n";
        ans = min(ans,t);
        //cout<<ans<<"|||\n";
    }
}
//int b[10005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //int x;
    cin>>n;
    int te = n;
    while(n){
        a[++ cnt] = n % 10;
        n /= 10;
    }
    n = te;
    reverse(a + 1,a + cnt + 1);
    if(check(n)) cout<<n;
    else{
        ans = 1e18;
        for(int i = -9;i <= 9;i ++ ){
            check(a[1],i);
            check(a[1] + 1,i);
        }
        cout<<ans<<"\n";
    }
    
}

F - Reordering
给出一串字符,求解该字符串的子集个数
组合数学+dp
第一维表示已经用了多少个字母表中的字母
第二维表示用了多少个字母
k是当前第i个字母表中的字母在串中出现的次数
那么可以推断 f [ i + 1 ] [ j ] = f [ i + 1 ] [ j ] + f [ i ] [ j − k ] ∗ C j k f[i + 1][j] = f[i + 1][j] + f[i][j - k] * C_{j}^{k} f[i+1][j]=f[i+1][j]+f[i][jk]Cjk

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define int long long
using namespace std;
const int mod = 998244353;
const int MX = 5005;
int f[30][MX];
int cnt[30];
int fac[MX];
int inv[MX];
int finv[MX];
void fac_init(){
    fac[0] = fac[1] = 1;
    inv[1] = 1;
    finv[0] = finv[1] = 1;
    for(int i = 2;i < MX;i ++ ){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - mod / i * inv[mod % i] % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}
int c(int x,int y){
    if(x < y || x < 0 || y < 0) return 0;
    return fac[x] * finv[y] % mod * finv[x - y] % mod;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    fac_init();
    string str;
    cin>>str;
    //cout<<str;
    int t = str.size();
    for(int i = 0;i < t;i ++ ){
        cnt[str[i] - 'a'] ++;
    }
    f[0][0] = 1;
    for(int i = 0;i < 26;i ++ ){
        for(int j = 0;j <= t;j ++ ){
            for(int k = 0;k <= min(j,cnt[i]);k ++ ){
                f[i + 1][j] += f[i][j - k] * c(j,k);
                f[i + 1][j] %= mod;
            }
        }
    }
    int ans = 0;
    for(int i = 1;i <= t;i ++ ){
        ans += f[26][i];
        ans %= mod;
    }
    cout<<ans;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道题的简要题解: A - Dodecagon 题目描述:已知一个正十二边形的边长,求它的面积。 解题思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 题目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,求最少需要多少个盘子才能装下所有的苹果。 解题思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 题目描述:给定一个长度为 $N$ 的整数序列 $a$,求除了第 $i$ 个数以外的最大值。 解题思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 题目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,求最少需要多少次操作才能完成任务。 解题思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值