Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))

Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))

A. Simply Strange Sort

暴力模拟即可

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;

int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }

const int maxn = 1e6 + 10;
const int N = 6e6 + 100;

int a[maxn];
vector<int> v[maxn];
vector<pair<int, int>> cave;

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <=n; ++i) cin >> a[i];
    int ans=0;
    while (!is_sorted(a+1,a+n+1)){
        for (int i = ans%2+1; i < n; i+=2) {
            if (a[i+1]<a[i]) swap(a[i],a[i+1]);
        }
        ans++;
    }
    cout<<ans<<endl;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




B. Charmed by the Game

根据总局数奇偶,
偶: 己方hold数为 0<i<a, 对方破发数为 a-i , 那么己方破发数为 sum/2 - i 及总数 sum/2 -i+a-i
奇: 。。。。

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 1e5 + 10;
const int N = 6e6 + 100;



void solve() {
    int a,b;
    cin>>a>>b;
    if (a>b) swap(a,b);
    int mid=(a+b)/2;
    vector<int > ans;
    if ((a+b)&1){
        for (int i = 0; i <=a; ++i) {
            ans.push_back(mid-i+a-i);
            ans.push_back(mid+1-i+a-i);
        }
    } else{
        for (int i = 0; i <=a; ++i) {
            ans.push_back(a-i+mid-i);
        }
    }
    cout<<ans.size()<<endl;
    sort(ans.begin(),ans.end());
    for(auto x:ans){
        cout<<x<<" ";
    }
    cout<<endl;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




C. Deep Down Below

求每个洞基本power,排序power遍历取最大power即可

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 1e5 + 10;
const int N = 6e6 + 100;

//int a[maxn];
vector<int> v[maxn];
vector<pair<int,int>> cave;

void solve() {
    int n;
    cin>>n;
    cave.clear();
    for (int i = 0; i < n; ++i) {
        int k;cin>>k;
        v[i].clear();
        for (int j = 0; j < k; ++j) {
            int arm;cin>>arm;
            v[i].push_back(arm);
        }
    }

    for (int i = 0; i < n; ++i) {
        int mip=0;
        for (int j = 0; j < v[i].size(); ++j) {
            v[i][j]-=j;
            mip= max(mip,v[i][j]+1);
        }
        cave.emplace_back(mip,v[i].size());
    }
    sort(cave.begin(),cave.end());
    int cnt=0,ans=0;
    for (int i = 0; i < n; ++i) {
        ans=max(ans,cave[i].first-cnt);
        cnt+=cave[i].second;
    }
    cout<<ans<<endl;
}

signed main() {
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




D1. Up the Strip (simplified version)

dp[n]=前面dp[i]和,dp[i/j]*(i/j-j)和

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 1e6 + 10;
const int N = 6e6 + 100;

//int a[maxn];
vector<int> v[maxn];
vector<pair<int,int>> cave;
int dp[maxn];

void solve() {
    int n,m;
    cin>>n>>m;
    dp[1]=1;
    int sum=1;
    for (int i = 2; i <=n; ++i) {
        dp[i]=(dp[i]+sum)%m;
        for (int j = 2,k; j <=i; j=k+1) {
            k=i/(i/j);
            dp[i]=(dp[i]+dp[i/j]*(k-j+1))%m;
        }
        sum=(sum+dp[i])%m;
    }
    cout<<dp[n];
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




D2. Up the Strip

将1中的求除数dp和换成对求后面的贡献,素数筛式dp

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 4e6 + 10;
const int N = 6e6 + 100;

//int a[maxn];
//vector<int> v[maxn];
//vector<pair<int,int>> cave;
int dp[maxn];
int f[maxn];

void solve() {
    int n,m;
    cin>>n>>m;
    dp[1]=1;
    for (int i = 2; i <=n; ++i) {
        f[i]=(f[i]+f[i-1]+dp[i-1]+1)%m;
        dp[i]=f[i];
        for (int j = 2*i; j <=n; j+=i) {
            f[j]=(f[j]+dp[i]-dp[i-1]+m)%m;
        }
    }
    cout<<dp[n];
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




E. Bottom-Tier Reversals

n个数
如下方法操作每回排2个需要5次操作, 一共 5n/2次
。。。。。n。。。。。
n。。。。。。。。。。
。。。。。n n-1 x 。。
x n-1 n 。。。。。。。
n n-1 x 。。。。。。。
。。。。。。。x n-1 n

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
//const int mod = 998244353;
const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
const int maxn = 4e6 + 10;
const int N = 6e6 + 100;

int a[maxn];
vector<int> v;

int f(int x,int n){
    for (int i = 1; i <=n; ++i) {
        if (x==a[i]) return i;
    }
}

void solve() {
   int n,flag=1;
   cin>>n;
   v.clear();
    for (int i = 1; i <=n; ++i) {
        cin>>a[i];
        if ((a[i]^i)&1) flag=0;
    }
    if (!flag){cout << -1<<"\n";return;}
    while (n>1){
        int x= f(n,n);
        reverse(a+1,a+1+x);
        v.push_back(x);
        x=f(n-1,n);
        reverse(a+1, a+x);
        v.push_back(x-1);
        reverse(a+1,a+x+2);
        v.push_back(x+1);
        reverse(a+1,a+4);
        v.push_back(3);
        reverse(a+1,a+n+1);
        v.push_back(n);
        n-=2;
    }
    cout<<v.size()<<"\n";
    for ( auto item : v) cout<<item<<" ";
    cout<<endl;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值