得分:
T1
思路:
标准的LIS(最长上升子序列),但是注意第二重循环枚举的是 的长度。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
string s[2005];
int ans;
int dp[10005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> s[i];
}
int maxx = -1e18;
for(int i = 1 ; i <= n ; i++){
dp[i] = 1;
for(int j = 0 ; j < i ; j++){
if(s[j] == s[i].substr(0,s[j].size())){
dp[i] = max(dp[j] + 1,dp[i]);
}
}
maxx = max(maxx,dp[i]);
}
cout << maxx;
return 0;
}T2
思路:
标准的BFS模板,注意要将出发点的步数设为0。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,x,y;
struct node{
int x,y,setp;
};
queue<node> q;
int vis[1005][1005];
void bfs(int x,int y){
q.push({x,y,0});
vis[x][y] = 1;
while(!q.empty()){
int x = q.front().x,y = q.front().y;
int setp = q.front().setp;
q.pop();
for(int i = -2 ; i <= 2 ; i++){
for(int j = -2 ; j <= 2 ; j++){
if(abs(i) + abs(j) != 3)continue;
int dx = x + i;
int dy = y + j;
if(dx > 0 && dx <= n && dy > 0 && dy <= m && vis[dx][dy] == 1e18){
vis[dx][dy] = min(vis[dx][dy],setp + 1);
q.push({dx,dy,setp + 1});
}
}
}
}
}
signed main(){
scanf("%lld %lld %lld %lld",&n,&m,&x,&y);
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
vis[i][j] = 1e18;
}
}
bfs(x,y);
vis[x][y] = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(vis[i][j] == 1e18){
printf("%-5d",-1);
}else{
printf("%-5lld",vis[i][j]);
}
}
cout << '\n';
}
return 0;
}
/*
0 3 2
3 -1 1
2 1 4
*/T3
思路:
大模拟,我们可以先准备一个空白的图,然后枚举图章的四个方向,然后再遍历整个图。如果某一个地方可以涂,就涂上。否则就看下一个点可不可以。最后检查一遍整个数组,如果有一个地方不符合目标图,就输出NO,否则输出YES。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
int n,m;
char a[105][105],b[105][105];
char c[105][105],d[105][105];
bool check(int x,int y){
for(int i1 = x,i2 = 1 ; i2 <= m ; i2++,i1++){
for(int j1 = y,j2 = 1 ; j2 <= m ; j2++,j1++){
if('.' == a[i1][j1] && b[i2][j2] == '*'){
return false;
}
}
}
return true;
}
void gai(int x,int y){
for(int i1 = x,i2 = 1 ; i2 <= m ; i2++,i1++){
for(int j1 = y,j2 = 1 ; j2 <= m ; j2++,j1++){
if(b[i2][j2] == '*'){
c[i1][j1] = '*';
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--){
cin >> n;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
cin >> a[i][j];
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
c[i][j] = '.';
}
}
cin >> m;
for(int i = 1 ; i <= m ; i++){
for(int j = 1 ; j <= m ; j++){
cin >> b[i][j];
}
}
for(int s = 1 ; s <= 4 ; s++){
char fz[105][105];
for(int i = 1 ; i <= m ; i++){
for(int j = 1 ; j <= m ; j++){
fz[j][m - i + 1] = b[i][j];
}
}
for(int i = 1 ; i <= m ; i++){
for(int j = 1 ; j <= m ; j++){
b[i][j] = fz[i][j];
}
}
for(int i = 1 ; i <= n - m + 1 ; i++){
for(int j = 1 ; j <= n - m + 1 ; j++){
if(check(i,j)){
gai(i,j);
}
}
}
}
// for(int i = 1 ; i <= n ; i++){
// for(int j = 1 ; j <= n ; j++){
// cout << c[i][j] << " ";
// }
// cout << '\n';
// }
bool f = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
if(c[i][j] != a[i][j]){
f = 1;
cout << "NO\n";
break;
}
}
if(f == 1)break;
}
if(f == 0)cout << "YES\n";
}
return 0;
}T4
思路:
DFS,我们可以枚举所有的缆车,如果有一个缆车可以载下第setp只小猫,就载上,然后递归下一层。对于每一种情况我们都可以开一个新的缆车,让第setp只小猫坐。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,w;
int c[100005];
int ans = 1e18;
bool vis[105];
int box[10005];
void dfs(int len,int setp){
if(len >= ans)return ;
if(setp > n){
ans = min(ans,len);
return ;
}
for(int i = 1 ; i <= len ; i++){
if(box[i] + c[setp] <= w){
box[i] += c[setp];
dfs(len,setp + 1);
box[i] -= c[setp];
}
}
len++;
box[len] += c[setp];
dfs(len,setp + 1);
box[len] -= c[setp];
}
bool cmp(int a,int b){
return a > b;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> w;
for(int i = 1 ; i <= n ; i++){
cin >> c[i];
}
sort(c + 1,c + 1 + n,cmp);
dfs(0,1);
cout << ans;
return 0;
}T5
思路:
我们可以尝试接解这个二元一次方程:
最后我们可以发现:答案(n)要不是4的倍数,要不是奇数。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ans = 0;
cin >> a >> b;
for(int i = a ; i <= b ; i++){
if(i % 4 == 0 || i % 2 != 0)ans++;
}
cout << ans;
return 0;
}T6
思路:
图论题,我们可以遍历一遍所有的点,让某一个不能走。再DFS一遍看能不能到达y点。不能就ans++。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int x,y;
bool vis[2005];
vector<int> a[2005];
int ans;
bool block[10005];
bool f;
void dfs(int x){
vis[x] = 1;
if(x == y){
f = 1;
return ;
}
int l = a[x].size();
for(int i = 0 ; i < l ; i++){
if(vis[a[x][i]] == 0 && block[a[x][i]] == 0){
dfs(a[x][i]);
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> m >> n;
for(int i = 1 ; i <= n ; i++){
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
cin >> x >> y;
dfs(x);
if(f == 0){
cout << -1;
return 0;
}
for(int i = 1 ; i <= n ; i++){
f = 0;
if(i == x || i == y)continue;
memset(vis , 0 , sizeof vis);
block[i] = 1;
dfs(x);
if(f == 0){
ans++;
}
block[i] = 0;
}
cout << ans;
return 0;
}