首页 > 其他分享 >Codeforces Round #725 (Div. 3) ABC(双指针)F

Codeforces Round #725 (Div. 3) ABC(双指针)F

时间:2022-10-27 18:13:06浏览次数:72  
标签:typedef ABC const LL cin long Codeforces 725 ll

https://codeforces.com/contest/1538

AB都没啥好说的,C卡了半天,F挺好写的,找到规律了就直接ac
1300的题目卡半天,1500的题目写的顺顺利利,真呆啊我

A. Stone Game

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL maxn,minn;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            if(i==1) maxn=a[1],minn=a[1];
            else maxn=max(maxn,a[i]),minn=min(minn,a[i]);
        }
        LL maxl,maxr,minl,minr;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==maxn) maxl=i;
            else if(a[i]==minn) minl=i;
        }
        maxr=n-maxl+1;
        minr=n-minl+1;
        //cout<<maxl<<" "<<maxr<<" "<<minl<<" "<<minr<<endl;
        LL ans=max(maxl,minl);
        LL ans2=max(maxr,minr);
        //cout<<ans<<" "<<ans2<<endl;
        LL ans3;
        if(minl<maxl) ans3=minl+maxr;
        else ans3=maxl+minr;
        cout<<min({ans,ans2,ans3})<<endl;
    }
    return 0;
}

B. Friends and Candies

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        if(sum%n!=0) cout<<"-1"<<endl;
        else
        {
            LL res=0;
            LL avg=sum/n;
            for(int i=1;i<=n;i++)
            {
                if(a[i]>avg) res++;
            }
            cout<<res<<endl;
        }
    }
    return 0;
}

C. Number of Pairs(双指针)

题目大意:

给定长度为n的数组a,在给定一个大小【l,r】;

问我们随便取两个数字相加起来的总数在这个范围内的数量有多少?

注意:这必须得是两个数,i,j不可以重叠。
input 
4
3 4 7
5 1 2
5 5 8
5 1 2 4 3
4 100 1000
1 1 1 1
5 9 13
2 5 5 1 1
output 
2
7
0
1

我一开始wa了半天是因为从后往前找的数量,后来过的时候是从前往后找的数量
其实道理应该都是一样,就是控制在O(n)的范围之内,然后要注意到比如这组数据:
5 5 8
1 2 3 4 5
当我们从前往后的时候,我们可以知道第一个1它的适配范围在[4,5];
2的适配范围在[3 ,4 ,5];
但是3的适配范围又在[4,5]。

  1. 我们可以从这组数据中发现,它的左边界是可以左右移动的,但是右边界不会(因为我的数字变大了之后,在之前的右边界的基础上只会越来越小)
  2. 同时还有一个地方就是它的左右边界一定是会在自己的右边的,就是说它不会再继续往前面找(前面的我们已经计算过了)。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n,l,r;
        cin>>n>>l>>r;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        LL ll=1,rr=n;
        LL sum=0;
        for(LL i=1;i<=n-1;i++)
        {
            if(a[i]+a[rr]>=l)
            {
                ll=max(i+1,ll);
                if(a[i]+a[ll]>=l)
                {
                    while(ll-1>i&&a[i]+a[ll-1]>=l) ll--;
                }
                else
                {
                    while(a[i]+a[ll]<l) ll++;
                }

                while(a[i]+a[rr]>r) rr--;
                //cout<<i<<" "<<ll<<" "<<rr<<endl;
                if(ll<=rr) sum+=(rr-ll+1);
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

F. Interesting Function

题目大意:

问l改变到r的数位总共变化了多少次?
input 
4
1 9
9 10
10 20
1 1000000000
output 
8
2
11
1111111110

打表找规律,暴力出奇迹。
我们可以知道全部的基础就是r-l;
然后可以分别寻找跨越2位,3位,4位。。。。。依次加1

  • 注意这类l,r的题目,一个非常有用的点就是f[r]-f[l]或者f[r]-f[l-1](依据情况而定)。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
LL f[N];
void init()
{
    for(LL i=1;i<=20;i++)
    {
        if(i==1) f[i]=1;
        else f[i]=f[i-1]*10;
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    init();
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL l,r;
        cin>>l>>r;
        string rr=to_string(r);
        LL sum=0;
        LL num=r-l;
        //cout<<num<<endl;
        for(LL i=rr.size();i>1;i--)
        {
            LL ans=r/f[i]-l/f[i];
            //cout<<ans<<endl;
            sum+=ans;
        }
        cout<<sum+num<<endl;
    }
    return 0;
}

标签:typedef,ABC,const,LL,cin,long,Codeforces,725,ll
From: https://www.cnblogs.com/Vivian-0918/p/16833227.html

相关文章

  • Codeforces Round #619 (Motarack's Birthday)
    题面DarkisgoingtoattendMotarack’sbirthday.DarkdecidedthatthegiftheisgoingtogivetoMotarackisanarrayaofnnon-negativeintegers.Darkcr......
  • Codeforces Round #829 (Div. 2)
    Contest链接E题意简述给长为\(n\)序列,随机等概率交换两个不同位置(\(i<j\))的值,要求\(a_i>a_j\)时才能交换。\(n\le200000\)像这个题但是强制要求\(a......
  • Codeforces 1672 E. notepad.exe
    题意这是一道交互题,有n个字符串,每个字符串长度:0-2000,n:0-2000有一个机器对他进行排版,你可以给他一个每行的最大宽度w,那么每行只能放长度为w的字符;每行相邻两个字符......
  • Codeforces Round #690 (Div. 3) F
    F.TheTreasureofTheSegments理解题意就是要让我们找一个线段+他相交的所有线段max我们暴力枚举线段然后用sum-不相交的不相交的就好算了只有两种情况一个线段左......
  • ABC141F
    假如某一位有奇数个\(1\),那么无论怎么拆分这一位都会有贡献。那么先把这些贡献加起来,然后去除掉这些位。发现剩下来的位都是偶数个\(1\),也就意味着,无论怎么拆分,拆分出......
  • Codeforces Round #830 (Div. 2) A-D
    比赛链接A题解知识点:贪心,数论。先求出序列最大公约数\(d\),如果为\(1\)直接输出\(0\)。否则,尝试用最后一个数操作,\(gcd(d,n)=1\)则可以,花费为\(1\)。否则......
  • Educational Codeforces Round 109 (Rated for Div. 2) D
    D.Armchairs我们发现性质这前面的0显然是给第一个1匹配而不会前面0的给第二个后面的给第一个显然不优有了这个性质我们就可以通过0来做文章要是这个位置是0我们显......
  • Codeforces Round #715 (Div. 2) C
    C.TheSportsFestival观察发现我们显然选择一个数字开始后我们拿周围的数字显然存在最优解(sort过)这样就很金典了n=2000我们显然可以暴力区间dp然后将转移只用从拿......
  • Codeforces Round #697 (Div. 3) D
    D.CleaningthePhone金典贪心吧先sort从大到小考虑12两种情况显然要是我们当前now+最大的一个1那我们就直接break了继续我们知道了我们现在+最大的一个1不够我们......
  • 等腰直角三角形ABC中,角A=90,AB=AC,J为BC中点,BD=CE,作AG垂直BE交BC于F,作FH垂直CD交BE于P。
    2022年10月25日22点53分END......