AtCoder Beginner Contest 367 (A-D)

比赛地址 : 

AtCoder Beginner Contest 367 - AtCoder

A

模拟

inline void solve(){
    int a , b, c ; cin >> a >> b >> c ;
    if(a==0) a = 24 ;
    if(c>b){
        // 只可能同一天
        if(a>b&&a<c) cout << "No" << endl ;
        else cout << "Yes" << endl ;
    }else{
        // 可能跨天
        if(a>b||a<c) cout << "No" << endl ;
        else cout << "Yes" << endl ;
    }
}

模拟

inline void solve(){
    string s ; cin >> s ;
    while(s.back()=='0') s.pop_back() ;
    if(s.back()=='.') s.pop_back() ;
    cout << s << endl ;   
}

C

简单的回溯

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define all(x) x.begin(), x.end()
#define int long long

const int N = 10 ;

int r[N] ;

vector<int> path ;
int n , k ; 

bool pd(){
    int sum = 0 ;
    for(int x : path) sum += x ;
    if(sum%k==0) return true ;
    else return false ;
}

void dfs(int i){// 第i个位置选x
    if(i==n+1 && pd()){
        for(int yss : path) cout << yss << " " ;
        cout << endl ;
        return ;
    }
    for(int j=1;j<=r[i];j++){
        path.pb(j) ;
        dfs(i+1) ;
        path.pop_back() ;
    }
}

inline void solve(){
    cin >> n >> k ;
    for(int i = 1 ; i <= n ; i ++) cin >> r[i] ;
    dfs(1) ;
}
 
signed main(){
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}

D

哈希表优化

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define all(x) x.begin(), x.end()
#define int long long

const int N = 4e5 + 10  ;

int n ,  m ; 
int a[N] ;
int ps[N] ;


inline void solve(){
    cin >> n >> m ;
    for(int i=1;i<=n;i++) cin >> a[i] ;
    // a[n] 是 n->1
    for(int i=n+1;i<=2*n;i++) a[i] = a[i-n] ;
    int ans = 0 ;
    // 前缀和的区间和为M的倍数 , 且t-s<=n-1 -> s>=t-n+1
    for(int i=1;i<=2*n;i++) ps[i] = (ps[i-1] + a[i]) % m ;
    map<int,int> mx ; 
    int res = 0 ;
    for(int i=1;i<=n;i++){// 枚举终点
        int b = ps[i-1] ;
        if(mx.find(b)!=mx.end()){//能找到
            res += mx[b] ;
        }
        mx[b]++ ;
    }
    map<int,vector<int>> mp ;
    // (b-a)%m==0
    for(int i=1;i<=2*n;i++){// 枚举终点
        int b = ps[i-1] ;
        if(mp.find(b)!=mp.end()){//能找到
            // i-j<=n-1 --> j>=i-n+1
            ans += mp[b].end() - lower_bound(all(mp[b]),i-n+1) ;
        }
        mp[b].push_back(i) ;
    }
    ans -= res ;
    cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值