中规中矩的前三题
题目链接
A
分类讨论就行
#include<bits/stdc++.h>using namespace std;#define int long long
#define PII pair<int,int>void solve()
{int n, k, x;cin >> n >> k >> x;if (x != 1) {cout << "YES" << '\n';cout << n << '\n';while (n--) {cout << 1 << ' ';}cout << '\n';return;}//x==1if (k == 1) {cout << "NO" << '\n';return;}//x==1,k!=1int p = n / 2;if (n % 2 == 0) {cout << "YES" << '\n';cout << p << '\n';while (p)cout << 2 << ' ', p--;cout << '\n';return;}if (k < 3) {cout << "NO" << '\n';return;}cout << "YES" << '\n';cout << p << '\n';while (p != 1)cout << 2 << ' ', p--;cout << 3 << '\n';return;
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t;cin >> t;while (t--) {solve();}return 0;
}
B
题目大意
给定 a , b , c a,b,c a,b,c三点坐标,问从 a a a分别到 b , c b,c b,c的最短路径的最大重合距离
思路
分类计算即可, b , c b,c b,c的 x , y x,y x,y值分别可能在 a a a的 x , y x,y x,y的同侧和异侧,仅同侧有贡献为 m i n ( a b s ( x c − x a ) , a b s ( x b − x a ) ) + m i n ( a b s ( y c − y a ) , a b s ( y b − y a ) ) min(abs(xc-xa),abs(xb-xa))+min(abs(yc-ya),abs(yb-ya)) min(abs(xc−xa),abs(xb−xa))+min(abs(yc−ya),abs(yb−ya))
#include<bits/stdc++.h>using namespace std;#define int long long
#define PII pair<int,int>void solve()
{int x1, x2, x3, y1, y2, y3;cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;x2 -= x1;y2 -= y1;x3 -= x1;y3 -= y1;int ans = 0;if (x2 * x3 > 0)ans += min(abs(x2), abs(x3));if (y2 * y3 > 0)ans += min(abs(y2), abs(y3));cout << ans + 1 << '\n';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t;cin >> t;while (t--) {solve();}return 0;
}
C
题目大意
给定字符串 s s s,给定长为 m m m的字符串 t 1 , t 2 t1,t2 t1,t2,问是否能找到长度为 m m m的字符串 b b b,使得 t 1 [ i ] < = b [ i ] < = t 2 [ i ] , 0 < = i < m t1[i]<=b[i]<=t2[i],0<=i<m t1[i]<=b[i]<=t2[i],0<=i<m,且 b b b不为 s s s的字串(允许不连续拼接)
思路
贪心即可
目的是构造一个合法字符串,而且在 s s s中可不连续拼接,所以遍历 t 1 , t 2 t1,t2 t1,t2,针对每一个 i i i,用 t 1 [ i ] t1[i] t1[i] ~ t 2 [ i ] t2[i] t2[i]的范围中的每一个数,从 s s s里找最后匹配,并保存位置,直到找不到匹配输出 Y E S YES YES,或全能找到匹配
#include<bits/stdc++.h>using namespace std;#define int long long
#define PII pair<int,int>bool solve() {string s;cin >> s;int m;cin >> m;string t1, t2;cin >> t1 >> t2;int j = 0;for (int i = 0;i < m;i++) {int r = 0;while (t1[i] <= t2[i]) {int tr = s.find(t1[i], j);if (tr == -1) return true;r = max(r, tr + 1);t1[i]++;}j = max(j, r);}return false;
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t;cin >> t;while (t--) {if (solve())cout << "YES" << '\n';else cout << "NO" << '\n';}return 0;
}