yz初赛总结
Xuan_He_OvO · · 个人记录
如题
答案在此
T1
思路
红绿之间转化,红变绿绿变红,中间需要用黄,两种情况:
- 中间是黄灯,两边不相等(两边不为黄);
- 两边是黄灯,中间任意(不为黄)。
如码所示
# include <bits/stdc++.h>
# define ll long long
//# define int long long
# define ull unsigned long long
# define endl '\n'
# define str string
# define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5;
const int P = 1e9 + 7;
int a, b, c;
int main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
fst;
cin >> a >> b >> c;
if ((! b) && (a != c)) {
cout << "YES" << endl;
return 0;
}
if ((! a) && (b) && (! c)) {
cout << "YES" << endl;
return 0;
}
cout << "NO" << endl;
return 0;
}
T2
人往往会把简单的问题复杂化然后再回过头来责怪题太难。
那可太好了,我是不可回收的fw。
思路
找规律,并不是多么复杂的规律,根据样例推出结论,注意输入的
分析样例1:
输入3,分为4份,4 ÷ 2 = 2。
样例3:
与样例1类似,输入7,分为8份,8 ÷ 2 = 4。
自己造数据: 分析样例2:
输入4,分为5份,4 + 1 = 5。
自己造数据:
解得:code =
# include <bits/stdc++.h>
# define ll long long
# define int long long
# define ull unsigned long long
# define endl '\n'
# define str string
# define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5;
const int P = 1e9 + 7;
ull n;
signed main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
fst;
cin >> n;
if (n == 0) {
cout << 0 << endl;
return 0;
}
n ++;
if (n % 2) {
cout << n << endl;
return 0;
}
else {
cout << (n / 2) << endl;
return 0;
}
return 0;
}
T3
思路
这是一道说难不难、说简单也不简单的结构体排序
没错所以还是简单
- 骗分:将所有
a_i 的值进行相加,再将这个和s 与t 进行对比如果可以输出0,即可获得10pts 的佳绩; - 再输出0后对结构体数组按照
a 的进行由大到小的排序; - 计算还需要多少支援;
- 根据题意进行模拟,注意再过程中对还需要的持续减少,如果不再需要就停止;
- 最后输出时需要判断还有没有需要的,如果有说明无解。
当思路为上述过程,code =
# include <bits/stdc++.h> # define ll long long //# define int long long # define ull unsigned long long # define endl '\n' # define str string # define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5; const int P = 1e9 + 7;
ll n, t; ll s; ll use; ll cost; ll need; ll ans;
struct node { ll a, b; } arr[N]; inline bool cmp (node x, node y) { return (x.a > y.a); }
int main () { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); fst; cin >> n >> t; for (int i = 1; i <= n; i ++ ) { cin >> arr[i].a; s += arr[i].a; } for (int i = 1; i <= n; i ++ ) { cin >> arr[i].b; } if (s >= t) { cout << 0 << endl; return 0; } sort (arr + 1, arr + 1 + n, cmp); use = (t - s); for (int i = 1; i <= n; i ++ ) { ll a = arr[i].a; ll b = arr[i].b; if (! a) { break; } if (use <= 0) { break; } if (! a) { cost = 0; } else { cost = (a * b); } if (cost >= use) { need = ((use + a - 1) / a); ans += need; use = 0; break; } else { ans += b; use -= cost; } } if (use > 0) { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
# T4
## 思路
用时最长的一题。
1. 骗分:由于题目中说可能不需要改变,所以直接输出0;
2. 每次翻转一个字符,会改变其与前后字符组成的子串,翻转位置$i$会影响$i - 1$和$i$组成的子串,以及$i$和$i + 1$组成的子串;
3. 记录到达每个位置,以“0”或“1”结尾的“01”和“10”子串数,以及所需的最小翻转次数。目标是找到一种状态,使得“01”和“10”子串数之和为3,且翻转次数最少。
## 易证
```cpp
# include <bits/stdc++.h>
# define ll long long
//# define int long long
# define ull unsigned long long
# define endl '\n'
# define str string
# define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5;
const int P = 1e9 + 7;
int n;
str s;
int c10, c01;
int f[5][2][600];
int w;
int j2;
int w2;
inline int cnt10 (str s) {
int cnt = 0;
for (int i = 0; i < (s.size () - 1); i ++ ) {
if ((s[i] == '1') && (s[i + 1] == '0')) {
cnt ++;
}
}
return cnt;
}
inline int cnt01 (str s) {
int cnt = 0;
for (int i = 0; i < (s.size () - 1); i ++ ) {
if ((s[i] == '0') && (s[i + 1] == '1')) {
cnt ++;
}
}
return cnt;
}
int main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
// fst;
cin >> n;
cin >> s;
c10 = cnt10 (s);
c01 = cnt01 (s);
if ((c10 + c01) == 3) {
cout << 0 << endl;
return 0;
}
for (int i = 0; i < 5; i ++ ) {
for (int j = 0; j < 2; j ++ ) {
for (int k = 0; k < 600; k ++ ) {
f[i][j][k] = INT_MIN;
}
}
}
{
if (s[0] == '0') {
f[1][0][0] = 1;
f[1][1][0] = 0;
}
else {
f[1][0][0] = 0;
f[1][1][0] = 1;
}
}
for (int k = 1; k < n; k ++ ) {
int f2[5][2][600];
for (int i = 0; i < 5; i ++ ) {
for (int j = 0; j < 2; j ++ ) {
for (int k = 0; k < 600; k ++ ) {
f2[i][j][k] = INT_MIN;
}
}
}
for (int i = 1; i <= 4; i ++ ) {
for (int j = 0; j <= 1; j ++ ) {
int num = f[i][j][k - 1];
if (num < 0) {
continue;
}
w = ((s[k] - '0') == j);
f2[i][j][k] = max (f2[i][j][k], (num + w));
if (i < 4) {
j2 = (1 - j);
w2 = ((s[k] - '0') == j2);
f2[i + 1][j2][k] = max (f2[i + 1][j2][k], (num + w2));
}
}
}
for (int i = 1; i <= 4; i ++ ) {
for (int j = 0; j <= 1; j ++ ) {
f[i][j][k] = f2[i][j][k];
}
}
}
cout << (n - max (f[4][0][n - 1], f[4][1][n - 1]));
return 0;
}
以下是不会的题
T5
思路
本
骗分之道
# include <bits/stdc++.h>
# define ll long long
# define int long long
# define ull unsigned long long
# define endl '\n'
# define str string
# define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5;
const int P = 1e9 + 7;
int n, k, a, b, x, y;
queue < int > q;
bool vis[N];
int f[N];
inline bool check (int x) {
if ((! vis[x]) && (x <= n) && (x >= 1)) {
return 1;
}
return 0;
}
inline void bfs () {
q.push (a);
vis[a] = 1;
f[a] = 0;
while (! q.empty ()) {
int u = q.front ();
q.pop ();
int s1 = u + x;
int s2 = u - y;
int s3 = u + (n / 2);
if (check (s1)) {
q.push (s1);
vis[s1] = 1;
f[s1] = (f[u] + 1);
}
if (check (s2)) {
q.push (s2);
vis[s2] = 1;
f[s2] = (f[u] + 1);
}
if (check (s3)) {
q.push (s3);
vis[s3] = 1;
f[s3] = (f[u] + 1);
}
if ((s1 == b) || (s2 == b) || (s3 == b)) {
cout << f[b] << endl;
return ;
}
}
cout << -1 << endl;
}
signed main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
fst;
cin >> n >> k >> a >> b >> x >> y;
bfs ();
return 0;
}
就这样,你得到了60由于是最后5min脑子一热想的,不再赘述
唉等等,主包突然发现自己忘了取模🙃
虽然加上也不一定对
修改之后如下码所示:
# include <bits/stdc++.h>
# define ll long long
# define int long long
# define ull unsigned long long
# define endl '\n'
# define str string
# define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5;
const int P = 998244353;
int n, k, a, b, x, y;
queue < int > q;
bool vis[N];
int f[N];
inline bool check (int x) {
if ((! vis[x]) && (x <= n) && (x >= 1)) {
return 1;
}
return 0;
}
inline void bfs () {
q.push (a);
vis[a] = 1;
f[a] = 0;
while (! q.empty ()) {
int u = q.front ();
q.pop ();
int s1 = u + x;
int s2 = u - y;
int s3 = u + (n / 2);
if (check (s1)) {
q.push (s1);
vis[s1] = 1;
f[s1] = (f[u] + 1);
f[s1] %= P;
}
if (check (s2)) {
q.push (s2);
vis[s2] = 1;
f[s2] = (f[u] + 1);
f[s2] %= P;
}
if (check (s3)) {
q.push (s3);
vis[s3] = 1;
f[s3] = (f[u] + 1);
f[s3] %= P;
}
if ((s1 == b) || (s2 == b) || (s3 == b)) {
cout << f[b] << endl;
return ;
}
}
cout << -1 << endl;
}
signed main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
fst;
cin >> n >> k >> a >> b >> x >> y;
bfs ();
return 0;
}
非常好,他确实不对
正解:
- 回顾一下bfs没错就是他帮主包得了60
pts 。 - 很明显我们可以看出本题不仅需要到达,还需要找最小时间,也就是最小价值。在原本
bfs 的基础上添加结构体,存储最小价值,如果最后无法到达输出-1 。得证
# include <bits/stdc++.h> # define ll long long # define int long long # define ull unsigned long long # define endl '\n' # define str string # define fst ios::sync_with_stdio (0); cin.tie (0); cout.tie (0);
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5; const int P = 1e9 + 7;
int n, k, a, b, x, y; int dis[N][15]; bool vis[N][15];
struct node { int pos; int cnt; };
inline int mod (int p) { return (((p - 1) % n + n) % n + 1); }
inline void bfs() {
// 原本就在:
if (a == b) {
cout << 0 << endl;
return;
}
// 基本初始化
{
memset (dis, -1, sizeof (dis));
queue < node > q;
q.push ({a, 0});
dis[a][0] = 0;
vis[a][0] = 1;
}
// bfs
while (! q.empty ()) {
// 基本取队首
{
node front = q.front ();
int u = front.pos;
int c = front.cnt;
q.pop();
}
// 已到达
if (u == b) {
cout << dis[u][c] << endl;
return ;
}
// 方案一
int s1 = mod (u + x);
if (dis[s1][c] == -1) {
dis[s1][c] = (dis[u][c] + 1);
q.push({s1, c});
}
// 方案二
int s2 = mod (u - y);
if (dis[s2][c] == -1) {
dis[s2][c] = (dis[u][c] + 1);
q.push({s2, c});
}
// 方案三
if (c < k) {
int s3 = mod (u + n / 2); // 注意!将你顺时针移动 n/2 颗行星,是原位置 + n / 2
if (dis[s3][c + 1] == -1) {
dis[s3][c + 1] = dis[u][c] + 1;
q.push({s3, c + 1});
}
}
}
// 到不了
cout << -1 << endl;
return ;
}
signed main () { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); fst; cin >> n >> k >> a >> b >> x >> y; bfs (); return 0; }
# T6
等等。。。我是不是没有脑子,怎么啥也没看懂
晚安玛卡巴卡!( ̄o ̄) . z Z
## 思路