Codeforces Round 869 (Div. 2) A~C

文章包含三道编程题目,分别涉及政治决策逻辑、全排列检查和子序列计算。第一题通过比较领导的表决权,找出相同意见的人数;第二题通过全排列检查找到特定整数排列模式;第三题使用二分查找优化计算几乎递增子序列的长度。
摘要由CSDN通过智能技术生成

A. Politics

思路:不难发现,跟1号(领导)表决权一样的留下,不一样的滚蛋

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;

void solve()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int cnt = 1;
    for(int i = 1; i < n; i ++ ){
    	string x;
    	cin >> x;
    	if(s == x) cnt ++;
	}
    cout << cnt << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t -- ) solve(); 
    system("pause");    
    return 0;
}

B. Indivisible

思路:其实写的时候我想了很多种构造方法都不太对,最后直接全排列数去check,找出了无比简单的规律。
其实道理还是挺简单的:
n是奇数由于全加起来一定会被n整除所以是-1
偶数:奇偶数相间排布保证连续的两个数不能被2整除,若按顺序奇偶排布,假设从头开始三个为一组,每组去均值之后是1 2 3,这样不合法。每个奇数和它后面的偶数交换位置,每组去均值之后是2 1 4,这样可以。这样排布,若以四个为一组,每组去均值之后都是2 1 4 3,和为10,不能被4整除……同理可证

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int n;
//2 1 4 3
//2 1 4 3 6 5
//2 1 4 3 6 5 8 7
//2 1 4 3 6 5 8 7 10 9

void solve()
{
    cin >> n;
    int sum = (n + 1)*n / 2;
    if(n == 1){
    	cout << 1 <<endl;
    	return;
	}
    if(sum % n == 0){
       cout << -1 << endl; 
       return;
    } 
    int a[n + 1];
    for(int i = 0; i < n; i ++ ) a[i] = i + 1;
    for(int i = 0; i < n; i += 2){
        swap(a[i], a[i + 1]);
    }
    for(int i = 0; i < n; i ++ ) cout << a[i] << ' ';
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t -- ) solve(); 
    system("pause");    
    return 0;
}

C. Almost Increasing Subsequence

思路:l~r的子序列长度是r-l+1,如果遇到一个almost-increasing就–。暴力一定会超时,所以需要首先预处理出来所有almost-increasing的区间(其实不用PII存也可以,因为almost-increasing的区间长度一定是3,如果有重叠就按两个区间来算)
然后就是二分查询的l和r之间有多少(m)个almost-increasing,答案就是r-l+1-m。

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 200010;
int n, q;
int a[N], idx;
PII b[N];

void solve()
{
    cin >> n >> q;
    for(int i = 0; i < n; i ++ ) cin >> a[i];
    for(int i = 2; i < n; i ++ ){
        if(a[i-2]>= a[i-1] && a[i-1] >= a[i]){
            b[idx].first = i-2;
            b[idx++].second = i;
        }
    }
    while(q -- ){
        int l, r;
        cin >> l >> r;
        if(l == r){
            cout << 1 << endl;
            continue;
        }
        if(r-l == 1){
            cout << 2 << endl;
            continue;
        }
        l--,r--;
        //二分出第一个大于l的b.first,第一个小于r的b.second
        //判断是否在l~r这个范围内
        int l1 = 0, r1 = idx - 1;
        int mid1;
        while(l1 < r1){
            mid1 = l1 + r1 >> 1;
            if(b[mid1].first < l) l1 = mid1 + 1;
            else r1 = mid1;
        }
        int l2 = 0, r2 = idx - 1;
        int mid2;
        while(l2 < r2){
            mid2 = l2 + r2 + 1>> 1;
            if(b[mid2].second > r) r2 = mid2 - 1;
            else l2 = mid2;
        }
        if(b[mid1].first < l) mid1 ++;
        if(b[mid2].second > r) mid2 --;
        int ma = 0;
        if(b[mid1].second > r) ma = 0;
        else if(b[mid2].first < l) ma = 0;
        else{
            ma = max(0, mid2 - mid1 + 1);
        }
//        cout << mid1 << ' ' << mid2 << ' ' << ma << endl;
        cout << r - l + 1 - ma << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
//    cin >> t;
    while(t -- ) solve(); 
    system("pause");    
    return 0;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜·肉多多·狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值