首页 > 其他分享 >Codeforces Round 861 (Div. 2)

Codeforces Round 861 (Div. 2)

时间:2023-04-01 16:33:12浏览次数:46  
标签:10 val 861 res Codeforces long int -- Div

题目链接

C

核心思路

这个思路说实话有点玄学,也就是我们前面的数位按照l或者r的相同数位来填补,后面就填相同的数字也就是比如l是2345

我们可以是2999,2888,23111,23777.

这样构造好像肯定是最小的。

但是好好巩固下数位dp来做这道题还是更好的。

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
using LL = long long;

int main(){

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    auto get = [&](LL x){
        int mn = 10, mx = -1;
        while(x){
            mn = min(1LL * mn, x % 10);
            mx = max(1LL * mx, x % 10);
            x /= 10;
        }
        return mx - mn;
    };

    int T;
    cin >> T;
    while(T--){
        LL l, r;
        cin >> l >> r;
        if (l < 10 || l == r){
            cout << l << '\n';
            continue;
        }

        int a[20], b[20];
        int len1 = 0, len2 = 0;
        LL num = l;
        while(num) a[++len1] = num % 10, num /= 10;
        num = r;
        while(num) b[++len2] = num % 10, num /= 10;
        int ans = 10;
        LL val = -1; 
        {
            int res = get(l);
            if (res < ans){
                ans = res;
                val = l;
            }
        }
        {
            int res = get(r);
            if (res < ans){
                ans = res;
                val = r;
            }
        }
        {
            LL x = 0;
            for(int i = len1; i >= 1; i--){
                for(int j = 0; j <= 9; j++){
                    LL t = x;
                    for(int k = i; k >= 1; k--)
                        t = t * 10 + j;
                    if (t < l || t > r) continue;
                    int res = get(t);
                    if (res < ans){
                        ans = res;
                        val = t;
                    }
                }
                x = x * 10 + a[i];
            }
        }
        {
            LL x = 0;
            for(int i = len2; i >= 1; i--){
                for(int j = 0; j <= 9; j++){
                    LL t = x;
                    for(int k = i; k >= 1; k--)
                        t = t * 10 + j;
                    if (t < l || t > r) continue;
                    int res = get(t);
                    if (res < ans){
                        ans = res;
                        val = t;
                    }
                }
                x = x * 10 + b[i];
            }
        }
        cout << val << '\n';
    }

}

D

核心思路

这个其实很明显是一个贡献题,我们可以先计算出来最坏的情况,就是每一个都需要修改。也就是\(ans=(n-k+1)*(k/2)\).然后如果发现有匹配的那么我们就ans--就好了。这又有个性质就是与某个点x匹配的点y必然是奇偶性相同的。

然后我们就是找对于任意一个数val看其左边有多少位置可以和它匹配。

然后就是经典的按照约束条件添加不等式就好了。

首先假设左端点是i,那么就有\((val+i)/2-k>=1\),\((val+i)/2+k<=n\).这是让我们的左右端点属于\([1,n]\).

然后就是 val和i的距离不可以超过k所以有\(val-i+1<=k\).还有一个就是\(1\leq i \leq val\).

接下来只需要把这些不等式联立就好了,然后再在我们预处理出来的数组中使用二分查询就好了。

// Problem: D. Petya, Petya, Petr, and Palindromes
// Contest: Codeforces - Codeforces Round 861 (Div. 2)
// URL: https://codeforces.com/contest/1808/problem/D
// Memory Limit: 256 MB
// Time Limit: 1500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
#define int long long 


const int N=2e5+10,mod=1e9+7;
int n,k;
vector<int> pos[N][2];

int cal(vector<int> &v,int l,int r)
{
	return upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l);
}



signed main()
{
cin>>n>>k;
if(k==1)
{
	cout<<0<<endl;
	return 0;
}
int ans=(n-(k-1))*(k/2);
int x;
for(int i=1;i<=n;i++)
{
	cin>>x;
	pos[x][i&1].push_back(i);
}
int maxx=2e5;
int res=0;
for(int val=0;val<=maxx;val++)
{
	for(int odd=0;odd<2;odd++)
	{
		for(auto i:pos[val][odd])
		{
		 int l = max(max(1ll*1, i - (k - 1)), 2 + k / 2 * 2 - i);
                int r = min(i - 1, 2 * n - k / 2 * 2 - i);
                if(l <= r)
                    ans -= cal(pos[val][odd], l, r);
		}
	}
}
cout<<ans<<endl;
return 0;

}

标签:10,val,861,res,Codeforces,long,int,--,Div
From: https://www.cnblogs.com/xyh-hnust666/p/17278821.html

相关文章

  • CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)(CF1810)A~D题题解
    今天采用的是新格式。CF1810ABeautifulSequence点击查看原题点击查看思路如果一个数字的值\(v\),不大于当前的位置\(p\),那我们可以通过删除\(p-v\)个数字,使它们两个对应上。比如\([1,7,2,5,3]\)中的\(3\),其数值为\(3\),位置为\(5\),数值\(3\)小于等于\(......
  • CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!) A-D题解
    题目地址A-BeautifulSequence题意:给出一个数组,问是否存在任意一个子区间,存在i,使得ai=iSolution直接比较当前的数和i的大小就行了,当前为x,如果要求答案存在,必须有i>=xvoidsolve(){ intn;cin>>n; intflag=0; for(inti=1;i<=n;i++) { intx;cin>>x; if(i>=x) {......
  • cf-div.2-860d
    题目链接:https://codeforces.com/contest/1798/problem/D贪心,比赛时一直搞C没搞出来,回头看D反而更简单。贪心策略:能填正数就填,填不了填负数。大致证明:构造的区间一定呈一个这样的特定区间,正...负正负负...负正....负负,证明一段区间为正且小于给定值易证,下面证最后一段区间的绝......
  • Codeforces Round 859 (Div. 4) ABCDE(交互题)FG1G2
    EFG1G2质量还挺好的A.PlusorMinushttps://codeforces.com/contest/1807/problem/A题目大意:给定a,b,c,问我们是a+b==c还是a-b==c?把正确的符号输出。input1112332129-7347112110336991899019-81910output+--++-++--+......
  • Codeforces Gym 103931F - Forest of Magic(时间轴分块+线段树合并)
    一个巨烦的时间轴分块做法,有点类似于P2137Gty的妹子树先考虑静态的情况。看上去就一脸线段树合并对吧?一次修改的操作对一个点\(x\)贡献可以写成\(k·dep_x+b\)的形式,开两棵线段树合并维护一次项和零次项系数即可。由于静态问题可做,因此考虑时间轴分块。设阈值\(B\),每\(B......
  • Div滚动到头以后置顶
    1<!DOCTYPEHTML>2<html>3<head>4<metacharset="utf-8"/>5<title>Div滚动到头以后置顶</title>6</head>7<bodystyle="height:2000px;">8<divstyle="height:200px&q......
  • cf-div2-860c
    题目链接:https://codeforces.com/contest/1798/problem/C大致题意:给你一个长度为\(n(n<=2e5)\)的序列的\(a_i,b_i\),让你把这个序列分成数目最少的段,每一段都有一个值\(c\),\(c=a_i的一个约数乘以b_i\)。比赛没写出的题。思路:\(首先一段里面的所有a_i*b_i能够整除c,易得c是所有b的......
  • 怎么改变div的位置、大小
    http://www.divcss5.com/rumen/r53973.shtml?ivk_sa=1024320uhttps://www.bbsmax.com/A/VGzl2Pk85b/ 可以使用css中的position来对div进行定位来改变div的位置,position属性值的含义。加上position:absolute,位置可以改变。    static:元素框正常生成。块级元素生成一个......
  • 如何让一个div拥有屏幕的高度
    有些时候登录页面需要设置一个图片有这个屏幕的大小但因为块级元素是根据自己内部的内容来撑起高度的当一开始没有内容或内容完全不足一页的时候就无法占据整个屏幕解决方法:将body,html,对应div的高度全部设置成为100%这样就可以让div撑满整个屏幕了。<body><divclass="ba......
  • Codeforces Round 860 (Div. 2)
    Preface两三天没写题了小小的补一下题结果这场意外地很合胃口,1h不到就把A-E做完了,而且除了忘记初始化这种一眼丁真的错误好像也没写挂可惜当时懒了周日晚上就不打了(主要......