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

AtCoder Beginner Contest 145

时间:2023-03-28 13:15:27浏览次数:55  
标签:AtCoder 145 infact Beginner int ll long ans mod

AtCoder Beginner Contest 145

https://atcoder.jp/contests/abc145

D - Knight

乍一看以为是dp,但是数据范围不允许。
仔细一看发现,两种操作的次数是固定的,可以枚举出来每种操作分别进行了多少次,如 \((1,2)\) 走了 \(a\) 次,总共走了 \(b\) 次,那么方案就是 \(\C_{b}^{a}\),组合数学小题。

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

using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, m;
ll ans, fact[N], infact[N];

int qmi(int a, int k, int p){
    int res = 1;
    while(k){
        if(k & 1)
            res = (ll)res * a % p;
        a = (ll)a * a % p;
        k >>= 1;
    }
    return res;
}

ll C(int a, int b) {
    return (ll)fact[a] * infact[b] % mod * infact[a - b] % mod;
}

int main () {
    fact[0] = infact[0] = 1;
    for(int i = 1; i < N; i++){
        fact[i] = (ll)fact[i-1] * i % mod;
        infact[i] = (ll)infact[i-1] * qmi(i, mod - 2,mod) % mod;
    }
    cin >> n >> m;
    //枚举(1,2)
    for (int i = 0; i <= n; i++) {
        int dx = n - i, dy = m - 2 * i; //还剩dx,dy
        if (dx < 0 || dy < 0)   continue;
        //剩下都走(2,1),走dy次
        if (dy * 2 != dx)   continue;
        // cout << i << ' ' << dy << endl;
        (ans += C (i + dy, i)) %= mod;
    }
    cout << ans << endl;
}

//枚举一种走多少次,看看剩下能走多少

E - All-you-can-eat

一开始记忆化搜索疯狂RE,原因是递归次数过多导致爆栈。
其实这题非常明显:每个物品只有选和不选————那不就是典型的01背包吗。
先排个序,体积小的在前面一定更优:

喜多私密马森hhh
然后直接背包就可以了,注意体积转移那里的边界:\(a_i+t-1\),因为题目说最后一个任务只要在ddl之前开始了就会一直执行下去

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

using namespace std;
typedef pair<int, int> pii;
const int N = 3005;
int n, t, f[N*2]; //f[j]: 考虑到第i个,耗时j
int ans;
pii p[N];

int main () {
    cin >> n >> t;
    for (int i = 1; i <= n; i++)    cin >> p[i].first >> p[i].second;
    sort (p + 1, p + n + 1);

    for (int i = 1; i <= n; i++) {
        for (int j = p[i].first + t - 1; j >= p[i].first; j--) {
            f[j] = max (f[j], f[j-p[i].first] + p[i].second);
        }
    }

    for (int i = 0; i < 6000; i++)  ans = max (ans, f[i]);
    cout << ans << endl;
}
 //01背包

F - Laminate

线性dp。
这个状态设计很神奇。
首先是问题转化:对于 \(a_i\),如果 \(a_i\leq a_{i-1}\),则消掉前面的时候会顺便把他消掉。若大于,可以选择直接删除
考虑怎么删除最优,则等价于留下的 \(n-k\) 个数消掉的代价最小
\(dp\) 状态设计:\(f_{i,j}:\) 选了 \(j\) 列,最左边那一列为 \(i\),这些列要消去所需的最小代价
转移:

\[f_{i,j}=\min_{p=i+1}^n f_{p,j-1}+\max (0,a_p-a_i) \]

注意初始化!!!

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

using namespace std;
const int N = 305;
int n, k, a[N];
ll f[N][N]; //f[i][j]: 选了j列,最左边那一列为i 的最小代价
ll ans = 1e18;


int main () {
    cin >> n >> k;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    if (n == k) {cout << 0; return 0;} //全部删掉
    memset (f, 0x3f, sizeof f);
    f[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - k; j++) {
            for (int t = 0; t < i; t++) {
                f[i][j] = min (f[i][j], f[t][j-1] + max (0, a[i] - a[t]));
            }
        }
        ans = min (ans, f[i][n-k]);
    }
    cout << ans;
}

//很多边界细节
//对于ai,如果ai<=a[i-1],则消掉前面的时候会顺便把他消掉,若大于,可以选择直接删除
//问题转化为n中选n-k个(不变)留下的最小代价

标签:AtCoder,145,infact,Beginner,int,ll,long,ans,mod
From: https://www.cnblogs.com/CTing/p/17264731.html

相关文章

  • AtCoder Beginner Contest 148
    AtCoderBeginnerContest148https://atcoder.jp/contests/abc148这场比较简单D-BrickBreak二分orLIS#include<bits/stdc++.h>#definelllonglongusingn......
  • 【题解】Atcoder ABC295 A-G
    A.ProbablyEnglish题目分析:直接每一个单词判一下就好了。代码:点击查看代码#include<bits/stdc++.h>usingnamespacestd;intmain(){ intn;scanf("%d",&n); bo......
  • AtCoder Beginner Contest 248 F(连通性状压dp)
    F连通性状压dp思路看了dls的讲解后才明白一点点。状态\(dp[i][j][k]\)表示到表示到i列,删除了j条边,点i和n-1+i是否联通,对于下一列点,若当前i和n-1+i连通,则多出来的三条......
  • AtCoder Educational DP Contest
    1.A没什么难度,直接算就可以了。点击查看代码#include<bits/stdc++.h>#defineintlonglong#defineYesprintf("Yes\n")#defineNoprintf("No\n")#defineYESpr......
  • Educational Codeforces Round 145 (Rated for Div. 2) A-D题解
    比赛地址A.Garland1voidsolve()2{3for(inti=1;i<=4;i++)4{5b[i]=a[i]=0;6}7intcnt=0;8stringt;cin>>t;9......
  • leetcode-1450-easy
    NumberofStudentsDoingHomeworkatGivenTimeGiventwointegerarraysstartTimeandendTimeandgivenanintegerqueryTime.Theithstudentstarteddoingt......
  • CF EC Round 145 D. Binary String Sorting
    D题意给一个01串,交换两个数需要花费\(10^{12}\),删除某个数需要花费\(10^{12}+1\),问最少花费多少使得串单调不降思路线性dp,\(f[i][0]\)表示前i位构建的串结尾为0,单调......
  • IU8689带主从模式,145W单声道&2X75W立体声D类音频功放
    IU8689E是一款单声道最高可输出145W,立体声2×75WD类音频放大器;这款器件在顶层设计了散热焊盘,焊盘上连接散热器后在供电电压24V的情况下,最大可以输出2×75W的连续功率;通过主......
  • AtCoder Grand Contest 019 F Yes or No
    洛谷传送门AtCoder传送门首先容易发现最优策略是回答剩余多的选项。设\(n\)为剩余Yes的数量,\(m\)为剩余No的数量,考虑将\((n,m)\)放到平面上,若\(n>m\)则回......
  • AtCoder Grand Contest 008 F Black Radius
    洛谷传送门AtCoder传送门神题!!!!111考虑如何不重不漏地计数。先考虑全为1的情况,令\(f(u,d)\)为与\(u\)的距离\(\led\)的点集。首先单独算全集,那么对于不是全集......