A
点击此处查看对应的题目.
本题涉及算法:数论知识
本题考察的是质数的性质,质数是只能被自身和1整除的数。所以必然不能是偶数那么将质数p mod 2 == 1,而另一个数则是p mod p-1 ==1。
时间复杂度 O ( 1 ) O(1) O(1)
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int p;
cin >> p;
cout << 2 <<" "<< p -1 <<'\n';
}
int main()
{
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
B
点击此处查看对应的题目.
本题涉及算法1:贪心
本题要求求出最大能留在第一行的人数,所以我们应该贪心的想依次枚举左,上,右三种情况,也就是尽量靠左站以保证站的人数最多。
时间复杂度 O ( n ) O(n) O(n)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
bool st[N];
void solve()
{
int n;
cin >> n;
string a,b;
cin >> a >> b;
for(int i = 0;i <= n;i ++) st[i] = false;
int res = 0;
for(int i = 0;i < n;i ++ ){
if(i - 1 >= 0 && !st[i - 1] && a[i - 1] == '1' && b[i] == '1'){
res ++;
st[i - 1] = true;
}else if(a[i] == '0' && !st[i] && b[i] == '1'){
res ++;
st[i] = true;
}else if(i + 1 <= n - 1 && !st[i + 1] && a[i + 1] == '1' && b[i] == '1'){
res ++;
st[i + 1] = true;
}
}
cout << res <<'\n';
}
int main()
{
int t;
cin >> t;
while(t --){
solve();
}
return 0;
}
C
点击此处查看对应的题目.
本题涉及算法:贪心
本题要维关键就是每次添加和删除我们只需要较强的那个贵族,较弱的那个贵族是不需要的.。
我的第一反应是用map来存较弱的那个贵族的出现次数,然后再遍历得到最后剩下的那个强者贵族。但这样会TLE ~~QAQ
所以我做了优化,每次出现一个较弱者就将从cnt++,这样求能取得一样的效果,然后用n - cnt即可。
时间复杂度 O ( q + m ) O(q + m) O(q+m)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> st(N,0);
int n,m,q,cnt;
void add(int a,int b)
{
if(st[a] == 0 && a < b) cnt ++;
else if(st[b] == 0 && a > b) cnt ++;
if (a < b) st[a] ++;
else if(a > b) st[b] ++;
}
void remove_edge(int a,int b)
{
if(st[a] - 1 == 0 && a < b) cnt --;
else if(st[b] - 1 == 0 && a > b) cnt --;
if(a < b) st[a] --;
else if(a > b) st[b] --;
}
int main()
{
cin >> n >>m;
while(m -- ){
int a,b;
cin >> a >> b;
add(a,b);
}
cin >> q;
while(q -- ){
int t;
cin >> t;
if(t == 1){
int a,b;
cin >> a >>b;
add(a,b);
}else if(t == 2){
int a,b;
cin >> a >> b;
remove_edge(a,b);
}else{
cout << n - cnt <<'\n';
}
}
return 0;
}