[比赛] Codeforces Round #538 (Div. 2) solution (贪心,数学其他,二分,线段树)

已经写了100篇题解啦!

link

solution pdf

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-‘0‘;c=getchar();}
    return ans*f;
}
int x,y,z,a,b,c,ans;
int main(){
    x=read(),y=read(),z=read(),a=read(),b=read(),c=read();
    ans=a+b+c;
    if(a<x){printf("NO");return 0;}
    ans-=x;
    a-=x;
    if(a+b<y){printf("NO");return 0;}
    ans-=y;
    if(ans<z){printf("NO");return 0;}
    else{printf("YES");return 0;}
}

A

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-‘0‘;c=getchar();}
    return ans*f;
}
const int N=200001;
int n,m,k;
struct node{
    int val,id;
}a[N];
bool cmp(node x1,node x2){return x1.val>x2.val;}
int s[N],ans;
signed main(){
    n=read(),m=read(),k=read();
    for(int i=1;i<=n;i++) a[i].val=read(),a[i].id=i;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=m*k;i++) s[++s[0]]=a[i].id,ans+=a[i].val;
    printf("%lld\n",ans);sort(s+1,s+s[0]+1);
    for(int i=1;i<k;i++) printf("%lld ",s[i*m]);
}

B

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<climits>
#define int long long
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-‘0‘;c=getchar();}
    return ans*f;
}
const int MAXN=1000001;
int n,k,num[MAXN],prime[MAXN],cnt,minn=LLONG_MAX;
signed main(){
    n=read(),k=read();
    for(int i=2;i<=sqrt(k);i++){
        if(k%i==0){
            prime[++cnt]=i;
            num[cnt]=1;k/=i;
            while(k%i==0){k/=i;num[cnt]++;}
        }
    }if(k>1){prime[++cnt]=k;num[cnt]=1;}
    for(int i=1;i<=cnt;i++){
        int N=n,sum=0;
        while(N) sum+=(N/prime[i]),N/=prime[i];
        sum/=num[i];
        minn=min(minn,sum);
    }printf("%lld\n",minn);
}

C

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-‘0‘;c=getchar();}
    return ans*f;
}
const int MAXN=5001;
int f[MAXN][MAXN],n,col[MAXN];
int main(){
    n=read();
    for(int i=1;i<=n;i++) col[i]=read();
    memset(f,127/3,sizeof(f));
    for(int i=1;i<=n;i++) f[i][i]=0;
    for(int len=2;len<=n;len++){
        for(int i=1;i+len-1<=n;i++){
            int j=i+len-1;
            if(col[i]==col[j]) f[i][j]=min(f[i][j],f[i+1][j-1]+1);
            if(col[i]==col[i+1]) f[i][j]=min(f[i][j],f[i+1][j]);
            else f[i][j]=min(f[i][j],f[i+1][j]+1);
            if(col[j]==col[j-1]) f[i][j]=min(f[i][j],f[i][j-1]);
            else f[i][j]=min(f[i][j],f[i][j-1]+1);
        }
    }printf("%d\n",f[1][n]);
}

D

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
inline int read(){
    int f=1,ans=0;char c;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-‘0‘;c=getchar();}
    return ans*f;
}
int ask1(int x){
    printf("? %d\n",x);fflush(stdout);
    int opt=read();
    return opt;
}
int ask2(int x){
    printf("> %d\n",x);fflush(stdout);
    int num=read();
    return num;
}
int qmax(){
    int l=0,r=1000000000,maxn=-INT_MAX;
    while(l<=r){
        int mid=(l+r)>>1;
        if(ask2(mid))maxn=max(maxn,mid),l=mid+1;
        else r=mid-1;
    }return maxn+1;
}int n,d,a[301];
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}int maxn;
signed main(){
    n=read();
    maxn=qmax();
    for(int i=1;i<=30;i++){
        int s=(rand()*rand())%n+1;
        a[++a[0]]=ask1(s);
    }sort(a+1,a+31);
    d=maxn-a[1];
    for(int i=1;i<=29;i++) d=gcd(d,a[i+1]-a[1]);
    printf("! %d %d\n",maxn-d*(n-1),d);fflush(stdout);
    return 0;
}

E

原文地址:https://www.cnblogs.com/si-rui-yang/p/10362873.html

时间: 2024-10-05 20:24:31

[比赛] Codeforces Round #538 (Div. 2) solution (贪心,数学其他,二分,线段树)的相关文章

Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x,3操作是将下标为k的数变为x. 注意成段更新的时候,遇到一个区间的最大值还小于x的话就停止更新. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的时间.然后有先手和后手俩个人. ?先手可以这么操作:在规定总时间T到达某个节点然后一定要返回根节点1,期间可以选择吃掉某些节点上的某些饼干(前提是保证剩下的时间能够回到根节点): ?后手可以这么操作:在先手到达的位置和这个位置的孩子之间的连边选择一条让先手吃得更多的边摧毁掉,也可以跳过这个过程: 问

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces Round #538 (Div. 2) (CF1114)

Codeforces Round #538 (Div. 2) (CF1114) ??今天昨天晚上的cf打的非常惨(仅代表淮中最低水平 ??先是一路缓慢地才A掉B,C,然后就开始杠D.于是写出了一个O(n^2)的线性dp,然后就wa6,调到结束.结束后发现完全看漏了两句话.噢,起始点!!! ??好吧然后算算自己有可能这一场要变成+0,反正在0左右. 结束后开始然后开始写D,顺便思考F.结果写完D发现A怎么fst了,然后...因为习惯于对相似的语句复制粘贴,有些东西没有改--三句话都在 -a!!!(

Codeforces Round 261 Div.2 D Pashmak and Parmida&#39;s problem --树状数组

题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到左预处理到某个下标为止有多少个数等于该下标,用map维护. 然后树状数组更新每个f(j,n,a[j]),预处理完毕,接下来,从左往右扫过去,每次从树状数组中删去a[i],因为i != j,i不能用作后面的统计,然后统计getsum(inc[a[i]]-1), (inc表示从左到右),即查询比此时的a[i]

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定