Codeforces Round 935 (Div. 3)

C. Left and Right Houses

大致意思就是让你选择一个位置铺路,使得道路左右两边的村庄中,至少一半的村民满意。然后给你一串字符,代表村民的意愿,0 代表居民想在路的左侧,1 代表想在右侧。

我们可以通过前缀和 O(1) 地得到每一个位置,左右两边村民的意愿 ,然后与人数比较,看是否满足条件。

#include <bits/stdc++.h> 
using namespace std;
const int mod = 1e9+7;
const double pi = acos(-1.0);
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int,int> pii;
inline int lowbit(int x) {return x & (- x);}


void solve()
{
    int n;cin >> n;
    string s;cin >> s;
    vector <int> a(n + 1);
    int idx = 0;
    for(int i = 0;i < n;i ++){
        if(s[i] == '1'){
            idx ++;
        }
        a[i + 1] = idx;
    }  
    // for(auto it : a) cout << it << " ";
    int ans = 1e9,mid = n;
    for(int i = 0;i <= n;i ++){
        int antl = i - a[i];//记录想在左边的村民的人数
        int antr = a[n] - a[i];//记录右边
        int idxl = ceil(1.0 * i / 2);//记录左边一半人数
        int idxr = ceil(1.0 * (n - i) / 2);
        // cout << antl << " " << antr << "\n";
        // cout << idxl << " " << idxr << "\n";
        if(antl >= idxl && antr >= idxr){
            if(abs(ans * 2 - mid) > abs(i * 2 - mid)){//乘以2,而不是除,不用取整
                ans = i;
            }
        }
    }
    cout << ans << "\n";
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

D. Seraphim the Owl

大致就是,将一个数插入到数组的m位之前,最少的花费数。

从后往前,依次比较a[i],b[i]答案取较小的。注意可以取到m位之前,如果值比较小。

#include <bits/stdc++.h> 
using namespace std;
const int mod = 1e9+7;
const double pi = acos(-1.0);
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int,int> pii;
inline int lowbit(int x) {return x & (- x);}
 
 
void solve()
{
    int n,m;cin >> n >> m;
    vector <int> a(n),b(n);
    for(int i = 0;i < n;i ++) cin >> a[i];
    for(int i = 0;i < n;i ++) cin >> b[i];
    ll ans = 0ll;
    ll res = a[m - 1];
    ll ant = 0ll;
    for(int i = n - 1;i  >= 1;i --){
        if(i > m - 1){
            ans += min(a[i],b[i]);//m位之后的数直接取较小的
            // cout << ans << "\n";
        }
        if(i <= m - 1){
            ant += b[i];
            if(ant + a[i - 1] < res){
                res = min(res,ant + a[i - 1]);
            }//m位之前的数如果有较小的,取最小的。
        }
    }
    ans += res;
    cout << ans << "\n";
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

E. Binary Search

大致就是让你对一组随机排列数,最多交换两次数字,使得二分搜索的结果是 x

贪心的想,我们先对原始排列进行一次二分搜索,将得到的位置与原数组的 的位置进行交换。

首先,原数组一定能够二分搜索到一个位置吗?

如果二分没写错的话。。。其实二分就是不断缩小搜索区间,而每一次的缩小只取决于 mid 上的数字的大小。

那么这样交换不会影响搜索的顺序吗?

假设我们最后搜索到位置 l 上的值为 p ,那么一定有 p <= x ,这是由我们的搜索条件限制的。

如果 p <= x ,那么交换 xp 的位置并不会影响最终的搜索顺序。

#include <bits/stdc++.h> 
using namespace std;
const int mod = 1e9+7;
const double pi = acos(-1.0);
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int,int> pii;
inline int lowbit(int x) {return x & (- x);}


void solve()
{
    int n,x;cin >> n >> x;
    vector <int> a(n + 1);
    int idx = 0;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
        if(a[i] == x) idx = i;
    }
    int l = 1, r = n + 1, m = 0;
    while(r - l > 1){//二分原数组
        m = (l + r) / 2;
        if(a[m] <= x){
            l = m;
        }else{
            r = m;
        }
    }
    cout << 1 << "\n";
    cout << idx << " " << l << "\n";
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

F. Kirill and Mushrooms

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值