复制代码

步梯 abc 367

Tasks - AtCoder Beginner Contest 367

A - Shout Everyday (atcoder.jp)

签到

#include <bits/stdc++.h>
typedef long long ll;
const int N = 2;

void solve()
{
    int a,b,c;
    std::cin >> a >> b >> c;
    bool ok = true;
    if(b > c) {
        if(a >= b && a <= 24) ok = false;
        if(a < c) ok = false;
    }
    else {
        if(a >= b && a <= c) ok = false;
    }
    if(ok) std::cout << "Yes\n";
    else std::cout << "No\n";
    return;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int _ = 1;
    //std::cin >> _ ;
    while(_ --) {
        solve();
    }
    return 0;
}

B - Cut .0 (atcoder.jp)

反转去除前导零

#include <bits/stdc++.h>
typedef long long ll;
const int N = 2e6 +50;

void solve()
{
    std::string s;
    std::cin >> s;
    std::reverse(s.begin(), s.end());
    int n = s.size();
    int cnt = 0;
    //std::cout << s << '\n';
    while(s.size())
    {
        if(s[0] != '0') break;
        if(s[0] == '.') {
            s.erase(0,1);
            break;
        }
        if(s[0] == '0') s.erase(0,1);
    }
    if(s[0] == '.') s.erase(0,1);
    std::reverse(s.begin(), s.end());
    std::cout << s << '\n';
    return;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int _ = 1;
    //std::cin >> _ ;
    while(_ --) {
        solve();
    }
    return 0;
}

C - Enumerate Sequences (atcoder.jp)

爆搜

#include <bits/stdc++.h>
typedef long long ll;
const int N = 3e5 + 50;
int a[N],R[N];
	int n,k;
	void dfs (int m, int s) {
		if(m > n) {
			if(s % k == 0) {
				for(int i = 1; i <= n; i++) 
					std::cout << a[i] << " ";
				std::cout << '\n';
			}
		}
		for(int i = 1; i <= R[m-1]; i++) {
			a[m] = i;
			dfs(m+1,s + i);
		}
	}

void solve() {

	std::cin >> n >> k;
	for(int i = 0; i < n; i++) std::cin >> R[i];
	dfs(1,0);
	return;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int _ = 1;
	//std::cin >> _;
	while(_ --) {
		solve();
	}
	return 0;
}

D - Pedometer (atcoder.jp)

考虑到s,t 在环上的大小关系分为两类

s<t 断环为链 Sumt1Sums1 % m = 0

(ab)modm=0 , a,bm 同余

枚举 t ,令 s=t1 ,cnt 统计相同余数

最巧妙的地方

for(int t = 2 ; t <= n; t++) {
    cnt[sum[t-2]] ++;// s -> t - 1 , s - 1 -> t - 2
    res += cnt[sum[t-1]]; // 保证 与t配对的s 均是小于t的
}

s>t

SumnSums1+Sumt1 = sufs+Sumt1

(a+b ) % m = 0, amodm+bmodm=m

同理枚举 s

#include <bits/stdc++.h>
typedef long long ll;
const int N = 2e6 + 50;

void solve()
{
    int n,m;
    std::cin >> n >> m;
    std::vector<int> a(n);
    std::vector<ll> sum(n+1),suf(n+2);
    std::map<int,int> cnt;
    for(int i = 0;i < n;i ++) {
        std::cin >> a[i];
        sum[i+1] = (sum[i] + a[i]) % m;
        //std::cout << sum[i+1] << " ";
    }
    //std::cout << '\n';
    for(int i = n ;i >= 1;i --) {
        suf[i] = (suf[i+1] + a[i-1]) % m;
        //std::cout << suf[i] << " ";
    }
    //std::cout << '\n';
    ll res = 0;
    for(int t = 2 ; t <= n; t++) {
        cnt[sum[t-2]] ++;// s -> t - 1
        res += cnt[sum[t-1]];
    }
    cnt.clear();
    for(int s = 2; s <= n; s++) {
        cnt[sum[s-2]] ++; // t -> s - 1
        res += cnt[(m - suf[s]) % m];// 防止后缀和取模等于0 产生cnt[m] 
    }
    std::cout << res << '\n';
    return;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int _ = 1;
    //std::cin >> _ ;
    while(_ --) {
        solve();
    }
    return 0;
}

E - Permute K times (atcoder.jp)

A7经过次变换值为Ax

经过一次 A6,经过两次A4 ,经过三次A3 , 经过四次A6

可以构建ix[i]基环树森林

每次转移就是在转移边

用倍增 st[i][0]=x[i] 表示i2i 转移后点是多少

k1018

st[i][j]=st[st[i][j1]][j1]

2j=2j1+2j1

经过2j1 次转移的点,再转移2j1

#include <bits/stdc++.h>
typedef long long ll;
const int N = 3e5 + 50;
int st[N][63];
void solve() {
	int n;
	ll k;
	std::cin >> n >> k;
	std::vector<int> X(n),A(n);
	for(int i = 0; i < n; i++) std::cin >> X[i];
	for(int i = 0; i < n; i++) std::cin >> A[i];

	for(int i = 1; i <= n; i++) {
		st[i][0] = X[i-1];
	}
	for(int j = 1; j <= 62; j ++) {
		for(int i = 1; i <= n; i++) {
			st[i][j] = st[st[i][j-1]][j-1];
		}
	}

	for(int i = 1; i <= n; i++) {
		int x = i;
		ll  y = k;
		for(int j = 62; j >= 0; j --) {
			if(y >= (1ll << j)) {
				x = st[x][j];
				y -= (1ll << j);
			}
		}
		std::cout << A[x-1] << " ";
	}
	return;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int _ = 1;
	//std::cin >> _;
	while(_ --) {
		solve();
	}
	return 0;
}

F - Rearrange Query (atcoder.jp)

  • Problem Statement

    You are given sequences of positive integers of length N: A=(A1,A2,,AN) and B=(B1,B2,,BN).

    You are given Q queries to process in order. The i-th query is explained below.

    • You are given positive integers li,ri,Li,Ri. Print Yes if it is possible to rearrange the subsequence (Ali,Ali+1,,Ari) to match the subsequence (BLi,BLi+1,,BRi), and No otherwise.

    哈希

    将每个i 映射到更大的范围,利用前缀和查询是否相等

    #include <bits/stdc++.h>
    typedef long long ll;
    const int N = 2e6 + 50;
    const ll base = 1145141;
    const ll mod = 1e9 + 7;
    void solve()
    {
        int n,q;
        std::cin >> n >> q;
        std::vector<int> a(n),b(n);
        std::vector<ll> p(n+1),ha(n+1,0),hb(n+1,0);
        for(int i = 0; i < n; i++) {
            std::cin >> a[i];
        }
        for(int i = 0; i < n; i++) {
            std::cin >> b[i];
        }
        p[0] = 1;
        for(int i = 1; i <= n; i++) {
            p[i] = p[i-1] * base % mod;
        }
        for(int i = 0; i < n; ++i) {
            ha[i+1] = (ha[i] + p[a[i]]) % mod;
        }
        for(int i = 0; i < n; ++i) {
            hb[i+1] = (hb[i] + p[b[i]]) % mod;
        }
        while(q--) {
            int l,r,L,R;
            std::cin >> l >> r >> L >> R;
            l--;
            L--;
            if((ha[r] - ha[l] + mod) % mod  == (hb[R] - hb[L] + mod) % mod) {
                std::cout << "Yes\n";
            }
            else {
                std::cout << "No\n";
            }
        }
        return;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        int _ = 1;
        //std::cin >> _ ;
        while(_ --) {
            solve();
        }
        return 0;
    }
    
    
posted @   Elgina  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示