2024天對程式設計天梯賽

chfychin發表於2024-04-21

L1-1 程式設計解決一切

程式設計解決一切
print('Problem? The Solution: Programming.')

L1-2 再進去幾個人

再進去幾個人
a, b = map(int, input().split())
print(b - a)

L1-3 幫助色盲

幫助色盲
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int a, b;
    string s1 = "", s2 = "";
    cin >> a >> b;
    if((a == 0||a == 1)&&b) {
    	s1 = "-";
    	s2 = (a ? "move" : "stop");
    } else if(a == 2) {
    	s1 = "-", s2 = "stop";
    } else {
    	s1 = (a ? "dudu" : "biii");
    	s2 = (a ? "move" : "stop");
    } cout << s1 << '\n' << s2 << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L1-4 四項全能

四項全能
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int n, m, x, s = 0;
    cin >> n >> m;
    for(int i = 0; i < m; i ++) {
    	cin >> x;
    	s += x;
    } cout << max(0ll, s - n * (m - 1)) << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L1-5 別再來這麼多貓娘了

別再來這麼多貓娘了(超時)
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int n, m;
    cin >> n;
    char c;
    getchar();
    vector<string> f(n);
    for(int i = 0; i < n; i ++) {
    	string s = "";
    	while((c = getchar()) != '\n') {
    		s += c;
    	} //cout << s << '\n';
    	f[i] = s;
    } cin >> m;
    getchar();
    string s = "", ans = "";
    while((c = getchar()) != '\n') {
    	s += c;
    } int ss = 0;
    for(int i = 0; i < n; i ++) {
    	while(s.find(f[i]) != string::npos) {
    		int l = s.find(f[i]);
    		int r = f[i].size() + l;
    		s = s.substr(0, l) + "<censored>" + s.substr(r);
    		ss ++;
    	}
    } if(ss < m) {
    	cout << s << '\n';
    } else {
    	cout << ss << "\nHe Xie Ni Quan Jia!\n";
    }
}

signed main() {
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L1-6 蘭州牛肉麵

蘭州牛肉麵
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int n;
    cin >> n;
    db s = 0;
    vector<db> f(n + 1, 0);
    vector<int> ff(n + 1, 0);
    for(int i = 1; i <= n; i ++) {
    	cin >> f[i];
    } int a, b;
    while(cin >> a >> b, a) {
    	ff[a] += b;
    	s += f[a] * b;
    } for(int i = 1; i <= n; i ++) {
    	cout << ff[i] << '\n';
    } cout << s << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(2);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L1-7 整數的持續性

整數的持續性
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int l, r, mx = 0;
    cin >> l >> r;
    for(int i = l; i <= r; i ++) {
    	string s = to_string(i);
    	int tt = 0;
    	while(s.size() > 1) {
    		int t = 1;
    		for(int j = 0; j < s.size(); j ++) {
    			t *= s[j] - '0';
    		} tt ++;
    		s = to_string(t);
    	} if(tt > mx) {
    		mx = tt;
    	}
    } cout << mx << '\n';
    int ok = true;
    for(int i = l; i <= r; i ++) {
    	string s = to_string(i);
    	int tt = 0;
    	while(s.size() > 1) {
    		int t = 1;
    		for(int j = 0; j < s.size(); j ++) {
    			t *= s[j] - '0';
    		} tt ++;
    		s = to_string(t);
    	} if(tt == mx) {
    		if(ok) {
    			cout << i;
    		} else {
    			cout << ' ' << i;
    		} ok = false;
    	}
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(2);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L1-8 九宮格

九宮格
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

int f[maxn][maxn];
int n, m;

inline void solve() {
    for(int i = 1; i <= 9; i ++) {
    	for(int j = 1; j <= 9; j ++) {
    		cin >> f[i][j];
    	}
    } for(int i = 1; i <= 9; i ++) {
    	vector<bool> st(n, false);
    	for(int j = 1; j <= 9; j ++) {
    		if(f[i][j] >= 1&&f[i][j] <= 9) {
    			st[f[i][j] - 1] = true;
    		} else {
    			cout << "0\n";
    			return ;
    		}
    	} for(auto x: st) {
    		if(!x) {
    			cout << "0\n";
    			return ;
    		}
    	}
    } for(int i = 1; i <= 9; i ++) {
    	vector<bool> st(n, false);
    	for(int j = 1; j <= 9; j ++) {
    		if(f[j][i] >= 1&&f[j][i] <= 9) {
    			st[f[j][i] - 1] = true;
    		} else {
    			cout << "0\n";
    			return ;
    		}
    	} for(auto x: st) {
    		if(!x) {
    			cout << "0\n";
    			return ;
    		}
    	}
    } for(int i = 1; i <= 9; i ++) {
    	vector<bool> st(9, false);
    	if(i == 1) {
    		for(int j = 1; j <= 3; j ++) {
    			for(int k = 1; k <= 3; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 2) {
    		for(int j = 1; j <= 3; j ++) {
    			for(int k = 4; k <= 6; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 3) {
    		for(int j = 1; j <= 3; j ++) {
    			for(int k = 7; k <= 9; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 4) {
    		for(int j = 4; j <= 6; j ++) {
    			for(int k = 1; k <= 3; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 5) {
    		for(int j = 4; j <= 6; j ++) {
    			for(int k = 4; k <= 6; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 6) {
    		for(int j = 4; j <= 6; j ++) {
    			for(int k = 7; k <= 9; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 7) {
    		for(int j = 7; j <= 9; j ++) {
    			for(int k = 1; k <= 3; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 8) {
    		for(int j = 7; j <= 9; j ++) {
    			for(int k = 4; k <= 6; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} else if(i == 9) {
    		for(int j = 7; j <= 9; j ++) {
    			for(int k = 7; k <= 9; k ++) {
    				if(f[j][k] >= 1&&f[j][k] <= 9) {
    					st[f[j][k] - 1] = true;
    				} else {
    					cout << "0\n";
    					return ;
    				}
    			}
    		}
    	} for(int j = 0; j < 9; j ++) {
    		if(!st[j]) {
    			cout << "0\n";
    			return ;
    		}
    	}
    } cout << "1\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    n = 9, m = 3;
    cin >> _;
    cout << fixed << setprecision(2);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L2-1 魚與熊掌

魚與熊掌
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
// #define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 1e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;
int n, m, root;

inline void solve() {
    cin >> n >> m;
    map<int, int> mp1[m + 10];
    for(int i = 1; i <= n; i ++) {
    	int k, x;
    	cin >> k;
    	while(k --) {
    		cin >> x;
    		mp1[x][i] ++;
    	}
    } int q;
    cin >> q;
    while(q --) {
    	int l, r, ans = 0;
    	cin >> l >> r;
    	for(auto t : mp1[l]) {
    		int x = t.x;
    		if(mp1[r].count(x)) {
    			ans ++;
    		}
    	} cout << ans << '\n';
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L2-2 懂蛇語

懂蛇語
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
// #define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

inline void solve() {
    int n, m, l = 0;
    cin >> n;
    map<string, int> mp1;
    vector<string> ans[n + 10];
    getchar();
    for(int i = 0; i < n; i ++) {
    	string s = "", t = "";
    	char c;
    	while((c = getchar()) != '\n') {
    		s += c;
    	} if(s[0] >= 'a'&&s[0] <= 'z') {
    		t = s[0] - 'a' + 'A';
    	} // cout << s << '\n';
    	for(int i = 1; i < s.size(); i ++) {
    		if(s[i - 1] == ' '&&s[i] >= 'a'&&s[i] <= 'z') {
    			t += s[i] - 'a' + 'A';
    		}
    	} if(mp1.count(t) == 0) {
    		mp1[t] = ++ l;
    		ans[l].emplace_back(s);
    	} else {
    		int tt = mp1[t];
    		ans[tt].emplace_back(s);
    	}
    } cin >> m;
    // for(auto x : ans) {
    	// for(auto y : x) {
    		// cout << y << '\n';
    	// } cout << "\n\n";
    // }
    getchar();
    while(m --) {
    	string s = "", t = "";
    	char c;
    	while((c = getchar()) != '\n') {
    		s += c;
    	} if(s[0] >= 'a'&&s[0] <= 'z') {
    		t = s[0] - 'a' + 'A';
    	} // cout << s << '\n';
    	for(int i = 1; i < s.size(); i ++) {
    		if(s[i - 1] == ' '&&s[i] >= 'a'&&s[i] <= 'z') {
    			t += s[i] - 'a' + 'A';
    		}
    	} if(mp1.count(t) == 0) {
    		cout << s << '\n';
    	} else {
    		int tt = mp1[t], ttt = ans[tt].size();
    		if(ans[tt].size() == 1) {
    			cout << ans[tt][0] << '\n';
    		} else {
    			sort(all(ans[tt]));
    			cout << ans[tt][0];
    			for(int i = 1; i < ttt; i ++) {
    				cout << '|' << ans[tt][i];
    			} cout << '\n';
    		}
    	}
    }
}

signed main() {
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L2-3 滿樹的遍歷

滿樹的遍歷
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
// #define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 1e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

vector<int> f[N], ans;
int n, root, t;
bool ok;

inline void dfs(int u) {
	if(f[u].size()) {
		if(t == 0) {
			t = f[u].size();
		} else if(f[u].size() != t) {
			t = max(t, (int)f[u].size());
			ok = false;
		}
	} sort(all(f[u]));
	ans.emplace_back(u);
	for(auto x : f[u]) {
		dfs(x);
	}
}

inline void solve() {
    cin >> n;
    ok = true;
    for(int i = 1; i <= n; i ++) {
    	int x;
    	cin >> x;
    	if(x == 0) {
    		root = i;
    	} else {
    		f[x].emplace_back(i);
    	}
    } dfs(root);
    cout << t << (ok ? " yes\n" : " no\n");
    cout << ans[0];
    for(int i = 1; i < ans.size(); i ++) {
    	cout << ' ' << ans[i];
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}

L2-4 吉利矩陣

吉利矩陣
#include <bits/stdc++.h>
#define arrout(a, l, r) rep(i, l, r) cout << a[i] << " \n"[i == r]
#define arrin(a, l, r) rep(i, l, r) cin >> a[i]
#define rep(i, l, r) for(int i = l; i <= r; i ++)
#define dep(i, l, r) for(int i = r; i >= l; i --)
#define erg(i, x, n) for(int i = h[x]; ~i; i = ne[i])
#define deb(x) cout << #x << " = " << x << '\n'
#define mem(a, x) memset(a, x, sizeof a)
#define all(f) f.begin(), f.end()
#define rall(f) f.rbegin(), f.rend()
#define all1(f) f.begin() + 1, f.end()
#define pii pair<int, int>
#define m_p make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define e_b emplace_back
#define ldb long double
#define db double
#define int long long
#define itn int
#define il inline
#define here system("pause")
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define MOD 998244353
#define mod 1000000007
#define endl "\n"
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

template <class T> inline void read(T& x) {x = 0;char c = getchar();bool f = 0;for(; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) {if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + 48);else write(x / 10), putchar(x % 10 + 48); }

inline int qmi(int a, int b, int p) {int ans = 1 % p;while(b){if(b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;} return ans;}
inline int inv(int a, int p) {return qmi(a, p - 2, p) % p;}

const int N = 2e5 + 10, M = 150, maxn = 20;
const double pi = acos(-1);
const long double E = exp(1);
const double eps = 1e-8;

bool st[maxn][maxn][maxn];
int s[maxn][maxn];
int n, m, ans;

inline void dfs(int x, int y) {
	if(x == m - 1&&y == m) {
		for(int i = 0; i < m; i ++) {
			int s1 = 0, s2 = 0;
			for(int j = 0; j < m; j ++) {
				s1 += s[i][j];
				s2 += s[j][i];
			} //cout << s1 << ' ' << s2 << '\n';
			if(s1 != n||s2 != n) {
				return ;
			}
		} ans ++;
		// for(int i = 0; i < m; i ++) {
			// for(int j = 0; j < m; j ++) {
				// cout << s[i][j] << " \n"[j == m - 1];
			// }
		// }
		return ;
	} if(y == m) {
		int s1 = 0;
		for(int i = 0; i < m; i ++) {
			s1 += s[x][i];
		} if(s1 != n) {
			return ;
		} x ++, y = 0;
	} for(int i = 0; i <= n; i ++) {
		if(!st[x][y][i]) {
			// int s1 = 0;
			// for(int j = 0; j < y; j ++) {
				// s1 += s[x][i];
			// } if(s1 + i > n) {
				// continue;
			// }
			st[x][y][i] = true;
			s[x][y] = i;
			dfs(x, y + 1);
			st[x][y][i] = false;
		}
	}
}

inline void solve() {
    int n, m, ans;
    cin >> n >> m;
    if(m == 2) {
    	ans = n + 1;
    } else if(m == 3) {
    	if(n == 2) {
    		ans = 21;
    	} else if(n == 3) {
    		ans = 55;
    	} else if(n == 4) {
    		ans = 120;
    	} else if(n == 5) {
    		ans = 231;
    	} else if(n == 6) {
    		ans = 406;
    	} else if(n == 7) {
    		ans = 666;
    	} else if(n == 8) {
    		ans = 1035;
    	} else {
    		ans = 1540;
    	}
    } else {
    	if(n == 2) {
    		ans = 282;
    	} else if(n == 3) {
    		ans = 2008;
    	} else if(n == 4) {
    		ans = 10147;
    	} else if(n == 5) {
    		ans = 40176;
    	} else if(n == 6) {
    		ans = 132724;
    	} else if(n == 7) {
    		ans = 381424;
    	} else if(n == 8) {
    		ans = 981541;
    	} else {
    		ans = 2309384;
    	}
    } cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(10);
    while(_ --) {
        solve();
    } return _ ^ _;
}
/*
24
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3
2 4
3 4
4 4
5 4
6 4
7 4
8 4
9 4
*/

L3-1 奪寶大賽

奪寶大賽
#include <bits/stdc++.h>

#define pii pair<int, int>
#define piii pair<pii, int>
#define x first
#define y second

#ifdef LOCAL
#include "algo/debug.h"
#else
#define dbg(...) "cyh2.2"
#define debug(...) "cyh2.2"
#endif

using namespace std;

const int N = 1e4 + 10, M = 110, maxn = 20;

bool ok;
int st[M][M],g[M][M];
int xx, yy, n, m;
int ne[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
vector<pii> f;

inline void bfs(int x, int y) {
	queue<piii> q;
	q.push({{x, y}, 0});
	while(q.size()) {
		auto t = q.front();
		q.pop();
		pii xx = t.x;
		int x = xx.x, y = xx.y, z = t.y;
		// st[x][y] = z;
		for(int i = 0; i < 4; i ++) {
			int nx = x + ne[i][0];
			int ny = y + ne[i][1];
			if(nx >= 0&&nx < n&&ny >= 0&&ny < m&&g[nx][ny] == 1&&!st[nx][ny]) {
				st[nx][ny] = z + 1;
				q.push({{nx, ny}, z + 1});
			}
		}
	}
}

inline void solve() {
	cin >> n >> m;
	for(int i = 0; i < n; i ++) {
		for(int j = 0; j < m; j ++) {
			cin >> g[i][j];
			if(g[i][j] == 2) {
				xx = i, yy = j;
			}
		}
	} bfs(xx, yy);
	int q;
	cin >> q;
	for(int i = 0; i < q; i ++) {
		int x, y;
		cin >> y >> x;
		x --, y --;
		if(st[x][y]) {
			f.emplace_back(i + 1, st[x][y]);
		}
	} if(f.size() == 1) {
		cout << f[0].x << ' ' << f[0].y <<'\n';
	} else if(f.size() > 1) {
		map<int, int> mp;
		for(int i = 0; i < f.size(); i ++) {
			mp[f[i].y] ++;
		} int mn = 1e9;
		for(auto [x, y] : mp) {
			if(y == 1) {
				mn = min(mn, x);
				ok = true;
			}
		} if(ok) {
			for(int i = 0; i < f.size(); i ++) {
				if(f[i].y == mn) {
					cout << f[i].x <<' ' <<f[i].y << '\n';
					return ;
				}
			}
		} else {
			cout <<"No winner.\n";
		}
	} else {
		cout <<"No winner.\n";
	}
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _ = 1;
    // cin >> _;
    cout << fixed << setprecision(0);
    while(_ --) {
        solve();
    } return _ ^ _;
}

相關文章