首页 > 其他分享 >Educational Codeforces Round 136 C-D

Educational Codeforces Round 136 C-D

时间:2022-09-30 10:22:05浏览次数:49  
标签:Educational const int Codeforces -- 136 mod 节点 define

D. Reset K Edges

显然对于每个k每个答案具有单调性
我们考虑二分一个能保留的最大长度 x
然后我们自上至下 肯定不好操作 我们考虑自下而上
对于每次到达了我们最大长度x 我们再断开是最优的
可以感性理解一下
因为我们越往上走 包括的节点就越多 而对于每个节点 被直接接到根节点肯定是更好的
最后我们要让这个点的高度变成0(对父节点是根节点的点 就不用了 反正都连在一起了)
这样return的时候 下一个节点才是高度1
时间复杂度是O(nlogn)

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
vector<int>g[N];
int n,k,h[N],ok;
void dfs(int u,int depth,int fa){
    h[u]=1;
    for(auto v:g[u]){
        dfs(v,depth,u);
        h[u]=max(h[u],h[v]+1);
    }
    if(h[u]==depth&&fa!=1){
        k--;
        if(k<0)ok = false;
        h[u]=0;
    }
}
void solve() {
    cin>>n>>k;
    for(int i=1;i<=n;i++)g[i].clear(),h[i]=0;
    for(int i=2;i<=n;i++){
        int u;cin>>u;
        g[u].push_back(i);
    }
    int l=1,r=n;
    while(l<r){
        int mid=l+r>>1;
        int cnt=k;
        ok=true;
        dfs(1,mid,1);
        if(ok)r=mid;
        else l=mid+1;
        k=cnt;
    }
    cout<<l<<endl;
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

C. Card Game

//只有我觉得C比D难嘛
我们观察样例 显然发现平局好像只有一种
我们构造平局 发现Alex必须拿到的牌必须是0110 0110 01....
我们假设Alex拿到的牌组是x 就是这个平局
因为每个牌的等级都不同(这样就可以看成是一个?进制
所以这个串是可直接比较的
我们手玩一下样例 也可以发现 只要小于x 并且 01数量相同 那就算一种方案
我们数位dp即可
最后别忘了减法的时候mod要加一个mod 不然就会和我一样罚时爆炸

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
int f[1010][1010];
void init(){
    for(int i=1;i<=1000;i++){
        for(int j=0;j<=i;j++){
            if(j==0||i==j)f[i][j]=1;
            else (f[i][j]=f[i-1][j-1]+f[i-1][j])%=mod;
        }
    }
}
int dp(int x){
    vector<int>nums;
    int last=0,res=0;
    while(x){if(x%2==1)last++;nums.push_back(x%2),x/=2;}
    for(int i=(int)nums.size()-1;i>=0;i--){
        int r=nums[i];
        if(r){
            (res+=f[i][last])%=mod;
            last--;
        }
    }
    return res;
}
void solve() {
    int n;cin>>n;
    int now=0;
    for(int i=0;i<n;i++){
        now<<=1;
        if(i%4+1==2||i%4+1==3){
            now+=1;
        }
    }
    int t=dp(now);
    cout<<(f[n][n/2]-t-1+mod)%mod<<' '<<t%mod<<' '<<1<<endl;
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        init();
        solve();
    }
    return ~~(0^_^0);
}

标签:Educational,const,int,Codeforces,--,136,mod,节点,define
From: https://www.cnblogs.com/ycllz/p/16744037.html

相关文章

  • Codeforces Round #822 (Div. 2)
    Preface这场间隔有点久了,ABC是上周日打的,DE是这周四写的感觉这场难度海星,比DE都不会的823友好多了A.SelectThreeSticks容易发现最终变成的长度一定是已经存在的,因......
  • *Codeforces Round #235 (Div. 2) C. Team(贪心)
    https://codeforces.com/contest/401/problem/C题目大意:给定n个0,m个1;让我们构建出一个字符串满足:不能连续2个以上的0,不能出现3个连续的1;可以的话就输出任意正确的结......
  • Codeforces Round #823 (Div. 2)
    B.MeetingontheLine题意:有n个人,第i个人的坐标是xi,从xi移动到yi要花|xi -yi|的时间。除此之外,他还需要ti 的时间打扮。试求一点使得所有人到这里所花时间的最大......
  • Educational Codeforces Round 135 (Rated for Div. 2) - E. Red-Black Pepper
    exgcdProblem-E-Codeforces题意给\(n\;(n<=3*10^5)\)个菜,每个菜可以加红辣椒或黑辣椒,分别可以获得\(c[i],d[i]\)分;有\(m\;(m<=3*10^5)\)个商店,第i个商店包......
  • 题解【CF1363F Rotating Substrings】
    CF1363FRotatingSubstrings*2600解题报告。不一定更好的阅读体验。感觉楼上的DP状态设计与DP转移方程的联系是说不通的,有些地方没有讲明白,所以这篇题解就详细......
  • Codeforces Round #240 (Div. 1) B. Mashmokh and ACM(DP)
    https://codeforces.com/contest/414/problem/B题目大意:给定一个范围【1,k】,要求我们从这里面选出n个数字,并且满足任意两个相邻数字中后一个数字%前一个数字==0问我......
  • Codeforces Round #105 (Div. 2) D. Bag of mice
    CodeforcesRound#105(Div.2)翻译岛田小雅D.Bagofmice出题人Nickolas巨龙和公主在纠结大年夜应该干什么。巨龙想去山上看精灵们在月光下跳舞,但公主只想早点睡......
  • Codeforces Round #823 (Div. 2)(持续更新)
    Preface本来没准备打这场比赛的,因为昨天出去high玩了一整天才发现今天才发现晚上有CF,遂报名rush一发结果今天状态有点崩,先是B看错题导致浪费时间,然后又是D自己叉自己把原......
  • Codeforces Round #822 (Div. 2) - E. Rectangular Congruence
    同余Problem-E-Codeforces题意给一个长度为\(n(2<=n<350)\)的数组\(b_i\),\(0<=b_0,b_1...b_n<n\)要构造一个大小为\(n*n\)的矩阵A,\(a_{i,i}=b_i\),并且满......
  • Codeforces Round #822 (Div. 2)
    D.SlimeEscape被greedy整破防了。这是转换后的题面。考虑使用调整法构造,记2个序列分别为\(f,g\),那么一种调整法是,\(f\)加了没事就加了不管,否则我们再考虑往当......