A - Different Divisors
思路:
1.数论简单题,用到了素数筛(或者线性筛)
2.我们观察样例,1肯定是选择的,最后一个因子肯定是a本身,然后中间两个是a的因子,我们可以发现,最好的情况这两个因子相乘等于a,这样才会使得a最小,为了保证因子之间的差值确定,那么这两个因子应该为素数
3.确定了这些,我们只需要从小到大枚举两个素数,乘积就是a
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define MAX_NUM 40000
typedef long long ll;
const int maxn=40000 + 7;
int t, d, a, k, x, ans;
int isprime[MAX_NUM+10];//最终如果isprime【i】为1,则表示i是素数
void prime()
{
for (int i = 2; i <= MAX_NUM; ++i)//开始假设所有数都是素数
isprime[i] = 1;
for(int i = 2; i <= MAX_NUM; ++i){//每次将一个素数的所有倍数标记为非素数
if (isprime[i])//只标记素数的倍数
for (int j = 2 * i; j <= MAX_NUM; j += i)
isprime[j] = 0;//将素数i的倍数标记为非素数
}
}
int main(){
prime();
scanf("%d", &t);
while(t--){
scanf("%d", &d);
x = d + 1;
for (int i = x; i <= maxn; ++i){
if(isprime[i] == 1){
k = i;
break;
}
}
for (int i = k + 1; i < maxn; ++i){
if(isprime[i] == 1 && i - k >= d){
a = i;
break;
}
}
ans = a * k;
printf("%d\n", ans);
}
return 0;
}
D - 这不是签到题
思路:数学分类讨论的思想。优先使得结果长度最大,然后优先让 a 数组每一位选1。
则 a数组的第一个数一定是1,之后每一位只要满足相加后结果不和上一位相同即可(都行则优先取1)
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 1e5 + 7;
string b;
ll t, n, j;
ll B[N], c[N], a[N];
int main(){
jiechufengyin;
cin >> t;
while(t--){
cin >> n;
cin >> b;
for(int i = 0; i < n; ++i){
B[i] = b[i] - '0';
// cout << B[i] << endl;
}
a[0] = 1;
c[0] = B[0] + a[0];
for(ll i = 1; i < n; ++i){
if(B[i] == 1){
if(c[i - 1] != 2){
a[i] = 1;
c[i] = 2;
}
else if(c[i -1] == 2){
c[i] = 1;
a[i] = 0;
}
}
else if(B[i] == 0){
if(c[i - 1] != 1){
a[i] = 1;
c[i] = 1;
}
else if(c[i - 1] == 1){
a[i] = 0;
c[i] = 0;
}
}
}
for(ll i = 0; i < n; ++i){
cout << a[i];
}
cout << endl;
}
return 0;
}
H - 最少拦截系统
思路:DP(最长子序列问题)。注意细节 !!
AC代码:
#include <iostream>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 1e3 + 7;
int ddnum, h, ans = 0, mmin, temp;
bool flag;
int height[100000];
int main(){
jiechufengyin;
while(cin >> ddnum){
ans = 0;
while(ddnum--){
cin >> h;
flag = 0;
mmin = inf;
for(int j = 0; j < ans; ++j){
if(h <= height[j] && mmin > height[j] - h){//不要再开一个拦截系统
mmin = height[j] - h;
temp = j;
flag = 1;
}
}
if(flag == 0){
height[ans] = h;
ans++;
}
else height[temp] = h;
}
cout << ans << endl;
}
return 0;
}
I - How Many Tables
简单并查集
AC代码:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
const int N = 1e3 + 7;
int t, n, m, f[N], a, b;
void init(int x){
for (int i = 1; i <= x; ++i){
f[i] = i;
}
return ;
}
int findfather(int v){
if(f[v] == v) return v;
else{
f[v] = findfather(f[v]);
return f[v];
}
}
void merge(int v, int u){
int t1, t2;
t1 = findfather(v);
t2 = findfather(u);
if(t1 != t2){
f[t2] = t1;
}
return ;
}
int main(){
cin >> t;
while(t--){
int sum = 0;
cin >> n >> m;
memset(f, 0, sizeof f);
init(n);
for (int i = 1; i <= m; ++i){
cin >> a >> b;
merge(a, b);
}
for (int i = 1; i <= n; ++i){
if(f[i] == i) sum++;
}
cout << sum << endl;
}
return 0;
}
K - 最短路径问题(Dijkstra)
Dijkstra模板,只是要统计时间和路程两个东西。但是输入的时候要特别注意,首先题目并没有说有重边,但是不判断重边会WA,还有就是先要保证在最短路经的情况下心求花费,而不是两个孤立的求,所以输入的时候要特别注意。
知识点:
1.fill()函数
头文件:algorithm
函数声明:fill(数组名,数组名 + n,值) ,值为任意值
作用:按单元填充,可以填充一个区间内所有的值
用法代码举例:
#include<iostream>
#include<algorithm>
using namespace std;
int a[5] = {1, 2, 3, 4, 5};
void print()
{
for(int i = 0; i < 5; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
cout << "原数组:" << endl;
print();
cout << "赋值为任意值的数组:" << endl;
fill(a, a + 5, 100);
print();
fill(a, a + 5, -1);
print();
return 0;
}
使用fill初始化二维数组:
法一:
fill(dis[0], dis[0]+maxn*maxn, INF);
法二:
for (int i = 1; i <= n; ++i){
fill(money[i], money[i] + n + 1, inf);
fill(mmap[i], mmap[i] + n + 1, inf);
}
2.Dijkstra算法(单源最短路)
具体知识点参考:https://blog.csdn.net/LXC_007/article/details/113390965
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
#define inf 0x3f3f3f3f
typedef long long ll;
ll mmap[N][N], book[N], dis[N], money[N][N];
ll n, m;
//初始化
void init(){
for (ll i = 1; i <= n; ++i){
fill(money[i], money[i] + n + 1, inf);
fill(mmap[i], mmap[i] + n + 1, inf);
}
fill(dis, dis + n + 1, inf);
fill(book, book + n + 1, inf);
}
void Dijkstra(int s){
queue<int> q;
dis[s] = 0;
book[s] = 0;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
for (ll i = 1; i <= n; ++i){
if(dis[i] > mmap[u][i] + dis[u]){
dis[i] = mmap[u][i] + dis[u];
book[i] = money[u][i] + book[u];
q.push(i);
}
else if(dis[i] == mmap[u][i] + dis[u]){
book[i] = min(book[i], money[u][i] + book[u]);
}
}
}
}
int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(cin >> n >> m){
if(n == 0 && m == 0) break;
init();
for (ll i = 1; i <= m; ++i){
ll a, b, c, d;
cin >> a >> b >> c >> d;
if(c < mmap[a][b]){
mmap[a][b] = mmap[b][a] = c;
money[a][b] = money[b][a] = d;
}
else if(c == mmap[a][b]){
if(d < money[a][b]) money[a][b] = money[b][a] = d;
}
}
int st, ed;
cin >> st >> ed;
Dijkstra(st);
cout << dis[ed] << ' ' << book[ed] << endl;
}
return 0;
}
注意:用cin,cout会被卡时间必须要解除封印或者用scanf和printf