Day1 AtCoder Beginner Contest 215 B-F

比赛链接

B - log2(N)

在这里插入图片描述
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=1e9+7;
const int N=1e5+10;
signed main()
{
    int n;
    cin>>n;
    int k=1;
    int cnt=0;
    while(k<n){
        k*=2;
        cnt++;
    }
    if(k>n) cout<<cnt-1<<endl;
    else cout<<cnt<<endl;
    return 0;
}

C - One More aab aba baa
在这里插入图片描述
题意:
输出给定字符串从小到大第n个字典序排列
题目分析:
先排序,然后调用全排列函数(好久没用有点遗忘),调用n次break输出即可
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=1e9+7;
const int N=1e5+10;
int a[15];
string s;
int n;
signed main()
{
    cin>>s>>n;
    for(int i=0;i<s.size();i++){
        a[i]=s[i]-'a';
    }
    sort(a,a+s.size());
    int sum=1*2*3*4*5*6*7*8;
    for(int i=1;i<=sum;i++){
        if(i==n) break;
        next_permutation(a,a+s.size());
    }
    for(int i=0;i<s.size();i++) cout<<char(a[i]+'a');
    return 0;
}

C - Coprime 2
在这里插入图片描述
题意:
输入n个数字和m,输出1-m中所有满足gcd(a[i],k)=1的k的取值
题目分析:
此题借用线性筛的思想,先对所有数分解质因子,然后对在1-m中筛去所有分解出的因数的倍数,剩下的就是k的所有取值
知识点:数论,gcd,分解质因数
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=1e9+7;
const int N=1e5+10;
int n, m, a[N];
bool st[N];
vector<int>ans;
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        for(int j=2;j<=a[i]/j;j++){
            if(a[i]%j==0){
                while(a[i]%j==0) a[i]/=j;
                st[j]=true;
            }
        }
        if(a[i]>1)st[a[i]]=true;
    }
    for(int i=1;i<=m;i++){
        for(int j=i;j<=m;j+=i){
            if(st[i]) st[j]=true;
            else break;
        }
    }
    for(int i=1;i<=m;i++)
        if(!st[i]) ans.pb(i);
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<endl;
    return 0;
}

D - Chain Contestant(待补)
在这里插入图片描述
题意:
给定一个给A-J组成的字符串,求子串数量,字串定义为出现的相同字母必须连续
题目分析:
状压dp,借用大佬博客传送门

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=998244353;
const int N=1100;
int dp[N][10];
int n,a[N];
char s[N];
signed main()
{
    fast;
    cin>>n;
    cin>>s+1;
    for(int i=1;i<=n;i++)
        a[i]=s[i]-'A';
    for(int i=1;i<=n;i++){
        int x=a[i];
        for(int j=0;j<1<<10;j++){
            if((j&1<<x)==0)continue;
            dp[j][x]=(dp[j][x]+dp[j][x])%mod;
            int state=j-(1<<x);
            for(int k=0;k<10;k++)
                dp[j][x]=(dp[j][x]+dp[state][k])%mod;
        }
        dp[1<<x][x]=(dp[1<<x][x]+1)%mod;
    }
    int ans=0;
    for(int i=0;i<1<<10;i++)
        for(int j=0;j<10;j++)
           ans=(ans+dp[i][j])%mod;
    cout<<ans<<endl;
    return 0;
}

F - Dist Max 2
在这里插入图片描述
题意:
求给定点之间定义距离的最大值
题目分析:
二分好题,除了想不到用二分不会写check。。。
距离单调,从[1-1e9]中二分出来答案,首先要明确check函数在什么情况下要更新,即满足 min ⁡ ( ∣ x i − x j ∣ , ∣ y i − y j ∣ ) \min \left(\left|x_{i}-x_{j}\right|,\left|y_{i}-y_{j}\right|\right) min(xixj,yiyj) >= mid,分情况讨论即可
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=998244353;
const int N=2e5+10;
PII a[N];
int n;
bool check(int mid)
{
    int minv=INF,maxv=-1;
    for(int i=1,j=1;i<=n;i++){
        while(j<=n&&a[i].fi>=a[j].fi+mid){
            minv=min(minv,a[j].se);
            maxv=max(maxv,a[j].se);
            j++;
        }
        if(a[i].se<=maxv-mid||a[i].se>=minv+mid)
            return true;
    }
    return false;
}
signed main()
{
    fast;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i].fi>>a[i].se;
    sort(a+1,a+1+n);
    int l=0,r=1e9;
    while(l<r){
        int mid=l+r+1>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值