题外话:
这场AB题不怎么难,但是到了C就做不动了,看了大佬的代码才勉强搞懂C题,D是没本事做出来了,直接放弃。
A. Gregor and Cryptography
分类:
简单思维
题目大意:
输入一个数p(p>5),找到a,b两个数,a,b大于等于2,小于等于p
我的思路:
这个题,直接分类讨论就可以
p为偶数,2和本身,余数都是0
p为奇数,2和p-1,余数都是1
我的代码:
//by lmy
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t, p, a, b;
cin >> t;
while (t--)
{
cin >> p;
if (p % 2 == 1)
cout << "2 " << p - 1 << "\n";
if (p % 2 == 0)
cout << "2 " << p << "\n";
}
return 0;
}
B. Gregor and the Pawn Game
题目大意:
有两个字符串数组a,b。
若b[i]==1&&a[i]==0
或b[i]==1&&a[i-1]==1
或b[i]==1&&a[i+1]==1
则b[i]可以移动到a中
求b中字符最多有几个可以移动到a
分类:
分类思想,思维
我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t, n, i;
string a, b;
cin >> t;
while (t--)
{
int sum = 0;
cin >> n >> a >> b;
a = "0" + a + "0";
b = "0" + b;
for (i = 1; i <= n; i++)
{
if (b[i] == '1')
{
if (a[i] == '0')
{
b[i] = '0';
sum++;
}
else if (a[i - 1] == '1')
{
a[i - 1] = '0';
sum++;
}
else if (a[i + 1] == '1')
{
a[i + 1] = '0';
sum++;
}
}
}
cout << sum << "\n";
}
return 0;
}
C. Web of Lies
分类:
图论
题目大意:
有n个人,他们从1-n编号为i,他们的力量也为i
这n个人之间有m种关系,比如1和2有关系,就代表1和2互为朋友
如果一个人,它有至少一个朋友,且他所有的朋友的力量都比他大,则他会被干掉。
你可以执行三种操作
- 在u和v之间添加关系
- 在u和v之间删除关系
- 输出还剩几个人
思路:
参考了大神的题解,才勉强懂了这题的意思
以下是大神的思路:
与一节点连接的所有节点的权值只有三种情况
1.有大有小
2.有大没小
3.有小没大
所以每次删除一个点,删除的都是有大没小的点,但是删除之后,会导致之前有大有小的点变成有大没小的节点
最终,只要这个节点连接有比它大的点,就会被删掉
因此,只需要统计有多少个结点,其连接有比其大的结点,之后减去就好了
至于图论是如何应用在其中的,有一说一,真没看出来,若有大佬明白,教教我鸭~
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn] = {0};
int main()
{
int n, m, q, u, v, judge;
int ans = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> u >> v;
if (u > v)
swap(u, v);
a[u]++;
if (a[u] == 1)
ans++;
}
cin >> q;
while (q--)
{
cin >> judge;
if (judge == 3)
cout << n - ans << "\n";
else
{
cin >> u >> v;
if (u > v)
swap(u, v);
if (judge == 1)
{
a[u]++;
if (a[u] == 1)
ans++;
}
else
{
a[u]--;
if (a[u] == 0)
ans--;
}
}
}
return 0;
}