好些天没打cf了,第一题差点给我劝退,硬看了30分钟,然后第二题本来很简单,然后想复杂了,第三题是最后几分钟才交上去,太长时间没打了,菜鸡 - > 超级菜鸡
一、A - Two 0-1 Sequences
- 题目:
给你两个01字符串a,b,问a是否可以执行操作变成b
操作是:a[1] = max(a[1],a[2])或者a[1] = min(a[1],a[2]),删除a[2],a中所有字符前移一位
- 思路
因为a只能删除前面的字符,所以就从右往左枚举b的每一个字符,只有第一个字符不相同的话a才可以使用操作寻找与b[1]想等的字符,否则其它位置的字符不相等的话就一定是不可能的
- 代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define pb push_back
#define int long long
#define PII pair<int,int>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 1e5 + 100,M = 1e5 + 100,mod = 1e9 + 7,INF = 0x3f3f3f3f;
void solve()
{
int n,m;cin >> n >> m;
string a,b;cin >> a >> b;
int f = 0 ;
if(n < m) f = 1;
else
{
int j = n - 1;
for(int i = m - 1;i >= 0;i -- )
{
if(b[i] == a[j]) j--;
else if(i == 0)
{
while(j >= 0 && a[j] != b[i])j --;
if(j == -1)
{
f = 1;
break;
}
}
else
{
f = 1;
break;
}
}
}
if(f) cout <<"NO" <<endl;
else cout <<"YES" <<endl;
//7 4
//1010001
//1001
}
signed main()
{
ios;int T;cin >> T;
while(T -- ) solve();
return 0;
}
二、B - Luke is a Foodie
- 题目:
给你一个数组a,长度是n,给你一个数字x,问你最少改变几次v的值使得满足 |v−ai| ≤ x的情况下 人能够从1走到n? - 思路
因为是从1走到n,不能改变顺序,所以模拟一下就行,就是每一个点v都有一个取值范围就是l = a[i] - x,r = a[i] + x,然后枚举每一个a[i]的范围,如果在范围外就ans++;
- 代码:
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define pb push_back
#define int long long
#define PII pair<int,int>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 2e5 + 100,M = 1e5 + 100,mod = 1e9 + 7,INF = 0x3f3f3f3f;
int a[N];
void solve()
{
int n,x; cin >> n >> x;
for(int i = 1;i <= n;i ++ ) cin >> a[i];
int ans = 0;
int l = a[1] - x,r = a[1] + x;
for(int i = 2;i <= n;i ++ )
{
int l1 = a[i] - x,r1 = a[i] + x;
if(l1 > r)
{
ans ++;
l = l1,r = r1;
}
else if(r1 < l)
{
r = r1;
l = l1;
ans++;
}
else
{
l = max(l1,l);
r = min(r1,r);
}
}
cout << ans << endl;
}
signed main()
{
ios;int T;cin >> T;
while(T -- ) solve();
return 0;
}
三、C - Virus
- 题目:
给你一个n,代表从1到n以圆的排列方式,给你m个被感染的点,感染点感染别的点的方式是感染自己的邻居,你每一天可以选择一个自己想要保护的点(这个点永远不可能被感染),在你的保护之下,最少能感染几个点? - 思路:
因为是一个圆形,所以先给m个感染的点排个序,(最后a[m + 1] = a[1]维持圆形),然后求出他们的差值 - 1,每两个被感染的点中间最多有差值 - 1个点被感染,一次感染被感染两个点,所以我们保护差值最大的点的邻居,被保护的点第一天最多只会被感染一个点,第二天就会被感染0个点,所以最多两天这两个数字中间的其他点不会被感染,其他的会被感染两个点,就从大到小枚举差值(贪心),然后计算
-代码:
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define pb push_back
#define int long long
#define PII pair<int,int>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 2e5 + 100,M = 1e5 + 100,mod = 1e9 + 7,INF = 0x3f3f3f3f;
int a[N];
struct node
{
int l,r;
}res[N];
bool cmp(node x,node y)
{
return x.l < y.l;
}
void solve()
{
int n,m; cin >> n >> m;
for(int i = 1;i <= m;i ++ ) cin >> a[i];
sort(a + 1,a + 1 + m);
a[m + 1] = a[1];
priority_queue<int,vector<int>,less<int> > res; // 可以不用优先队列,直接排序就行
for(int i = 2;i <= m + 1;i ++ )
if((a[i] - a[i - 1] - 1 + n) % n != 0) // (x + n ) % n 因为是一个圆
res.push((a[i] - a[i - 1] - 1 + n) % n);
int sum = m;
int d = 0;
int f = 0;
while(res.size())
{
int t = res.top();res.pop();
if(f) sum += t;// 如果这个点已经是被感染完了,那后面的就不用算了肯定也已经被感染了,
else
{
int u = t - d * 4; // 这是没两天计算一次,所以会最多感染4个点
if(u <= 0)
{
f = 1;
sum += t;
}
else
{
sum += d * 4;
if(u != 1) // 如果只剩下一个点没有被感染,你保护了他,就不用再 + 1 , + 1是第一天你选的点,最多会被感染1个.
sum++;
d++;
}
}
}
cout << sum << endl;
}
signed main()
{
ios;int T;cin >> T;
while(T -- ) solve();
return 0;
}
四、D - Magical Array
- 题目:
给你n个数组C(操作完以后的),每个数组的长度都是m,选择第k个数组C[k]为特殊序列,每个序列肯定会执行以下操作,问你哪一个序列是特殊序列,并且操作了几次
非特殊序列 ,选择一个i 和 j (i != j),然后b[i] = b[i] - 1,b[j] = b[j] - 1,b[i - 1] = b[i - 1] + 1,b[j + 1] = b[j + 1] +1
特殊序列 选择一个i 和 j (i != j),然后b[i] = b[i] - 1,b[j] = b[j] - 1,b[i - 1] = b[i - 1] + 1,b[j + 2] = b[j + 2] +1
- 思路:
对于非特殊序列所有前缀和的和不变,对于特殊序列操作一次所有前缀和的和减1,所以累计所有前缀和的和,然后求出最大值和最小值,然后相减就是答案 具体可看这位大佬的讲解
- 代码:
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define pb push_back
#define int long long
#define PII pair<int,int>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 3e5 + 100,M = 4e5 + 100,mod = 10007,INF = 0x3f3f3f3f;
void solve()
{
int m,n; cin >> n >> m;
vector<vector<int> > res(n + 1,vector<int> (m + 1));
vector<int> sum(n + 1);
for(int i = 1;i <= n;i ++ )
{
int s = 0;
for(int j = 1;j <= m;j ++ )
{
cin >> res[i][j];
s += res[i][j];
sum[i] += s;
}
}
int mx = -1,mi = 1e18,id = 0;
for(int i = 1;i <= n;i ++ )
{
if(sum[i] > mx) mx = sum[i];
if(sum[i] < mi)
{
mi = sum[i];
id = i;
}
}
//cout << mx <<' ' << mi << endl;
cout << id << ' ' << mx - mi << endl;
}
signed main()
{
ios;int T; cin >> T;
while(T -- ) solve();
return 0;
}
五、E - Count Seconds
- 题目:
给你一个有向无环图,每个点都有一个权值a[i],每一秒对于所有的a[i] > 0的点,a[i] = a[i] - 1,i 这个点连接的子节点y a[y] = a[y] + 1,问你所有的点的权值为0,需要多少秒? - 思路:
所有的点实际上都是在流通,只有出度为0的点才会流出,也就是整个系统的权值都是靠出度为0的那个点流出的,关于权值为0的点,首先必须要流通n秒,让整个系统的权值尽量往树的下方累计,然后dfs求出整体需要多少秒 + n就行,具体可看第四题的链接
- 代码:
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define pb push_back
#define int long long
#define PII pair<int,int>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 3e3 + 100,M = 4e5 + 100,mod = 998244353,INF = 0x3f3f3f3f;
vector<int> g[N],r[N];
int f[N];
vector<int> a,t,vis;
int dfs(int u)
{
if(vis[u]) return f[u];
vis[u] = 1;
f[u] = a[u];
for(auto x:r[u])
f[u] = (f[u] + dfs(x)) % mod;
return f[u];
}
void solve()
{
int m,n; cin >> n >> m;
a = vis = vector<int>(n + 1,0);
for(int i = 1;i <= n;i ++ ) cin >> a[i],g[i].clear(),r[i].clear(),f[i] = 0;
while(m -- )
{
int u,v; cin >> u >> v;
g[u].pb(v);
r[v].pb(u);
}
if(count(a.begin() + 1,a.end(),0) == n) {cout << "0" << endl; return;}
for(int i = 1;i <= n;i ++ )
{
auto t = a;
for(int u = 1;u <= n;u ++ )
{
if(a[u] > 0)
{
t[u]--;
for(auto x:g[u]) t[x]++;
}
}
a = t;
if(count(a.begin() + 1,a.end(),0) == n) {cout << i << endl; return;}
}
int root;
for(int i = 1;i <= n;i ++ )if(!g[i].size()) root = i;
cout << (dfs(root) + n) % mod << endl;
}
signed main()
{
ios;int T; cin >> T;
while(T -- ) solve();
return 0;
}