Educational Codeforces Round 50 (Rated for Div. 2) (A,B,C,D)

A. Function Height

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;


int main(){
    ll n , m;
    scanf("%lld%lld", &n, &m);
    if (m % n == 0)  cout << m/n << endl;
    else  cout << m/n + 1 << endl;
    return 0;
}

B. Diagonal Walking v.2

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;

int main(){
    int T;
    cin >> T;
    while (T -- ){
        ll n , m , k;
        scanf("%lld%lld%lld", &n, &m, &k);
        ll t = max(n, m) , ans;
        if (k < t){
            puts("-1");
            continue;
        }
        if (abs(n-m)%2 == 0){
            if (k % 2 == t % 2)  ans = k;
            else  ans = k - 2;
        }
        else ans = k - 1;
        cout << ans << endl;
    }
    return 0;
}

C. Classy Numbers

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;

int a[20], dp[20][5];

ll dfs(int pos, int sta, int limit) {
    if(pos < 0 && sta >= 0) return 1;
    if(pos < 0 || sta < 0) return 0;
    if(!limit && dp[pos][sta] >= 0) return dp[pos][sta];
    int up = limit ? a[pos] : 9;
    ll ans = 0;
    for(int i = 0; i <= up; ++i) {
        ans += dfs(pos - 1, sta - (i ? 1 : 0), limit && i == up);
    }
    if(!limit) dp[pos][sta] = ans;
    return ans;
}

ll so(ll x) {
    int p = 0;
    while(x) {
        a[p++] = x % 10;
        x /= 10;
    }
    return  dfs(p - 1, 3, 1);
}

int main() {
    memset(dp, -1, sizeof(dp));
    int T;
    scanf("%d", &T);
    while(T--) {
    	ll A, B;
	    scanf("%lld %lld", &A, &B);
	    printf("%lld\n", so(B) - so(A - 1));
	}
    return 0;
}

D. Vasya and Arrays

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 3e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;

int n, m;
int a[maxn], b[maxn];
ll an[maxn], bn[maxn];

int main(){
    while(scanf("%d", &n) != EOF){
        clr(a); clr(b); clr(an); clr(bn);
        for(int i=1; i<=n; i++) {
            scanf("%d", &a[i]);
            an[i] = an[i-1] + a[i];
        }
        scanf("%d", &m);
        for(int i=1; i<=m; i++) {
            scanf("%d", &b[i]);
            bn[i] = bn[i-1] + b[i];
        }
        int pa = 1, pb = 1;
        int cnt = 0;
        while(pa <= n && pb <= m){
            if(an[pa] == bn[pb]){
                cnt ++;
                pa ++;
                pb ++;
            }
            else if(an[pa] > bn[pb]) pb ++;
            else pa ++;
        }
        if(an[n] != bn[m]) puts("-1");
        else printf("%d\n", cnt);
    }
    return 0;
}

 

    原文作者:B树
    原文地址: https://blog.csdn.net/l18339702017/article/details/82529365
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞