1: P2392
左右脑同时计算题目 但仅限于一科
解决:回溯搜索
代码:
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<stdio.h>
int min1;
int arr[5];
int brr[21][5];
int left1 = 0;int right1=0;
using namespace std;
void search(int a, int b)
{
if (a > arr[b])
{
min1 = min(min1, max(left1, right1));
return ;
}
left1 += brr[a][b];
search(a + 1, b);
left1 -= brr[a][b];
right1 += brr[a][b];
search(a + 1, b);
right1 -= brr[a][b];
}
int main()
{
int sum = 0;
int i = 1, j = 1;
for (i = 1; i <= 4; i++)
{
cin >> arr[i];
}
for (i = 1; i <= 4; i++)
{
min1 = 1213132;
left1 = 0;
right1 = 0;
for (j = 1; j <= arr[i]; j++)
{
cin >> brr[j][i];
}
search(1, i);
sum += min1;
}
cout << sum;
return 0;
}
2: P1873
伐木机升起一个巨大的锯片到高度 HH,并锯掉所有树比 HH 高的部分。请帮助 Mirko 找到伐木机锯片的最大的整数高度 HH,使得他能得到的木材至少为 MM 米
解决:二分
代码:#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long arr[1000010];
int cmp(int a,int b)
{
return a<b;
}
int main()
{
long long n,i,mid,left,right;long long m,sum=0;
cin>>n>>m;
for(i=1;i<=n;i++)
cin>>arr[i];
sort(arr+1,arr+n+1,cmp);
left=0;right=arr[n];
while(left<=right)
{
sum=0;
mid=(left+right)/2;
for(i=1;i<=n;i++)
{
if(arr[i]>mid)
sum+=arr[i]-mid;
}
if(sum==m)
{
break;
}
else if(sum<m)
{
right=mid-1;
}
else if(sum>m)
{
left=mid+1;
}
}
if(sum==m)
cout<<mid;
else
cout<<left-1;
return 0;
}
3:p5143P51p是wds
他在地形图上标记了 NN 个点,每个点 P_iPi 都有一个坐标 (x_i,y_i,z_i)(xi,yi,zi)。所有点对中,高度值 zz 不会相等。HKE 准备从最低的点爬到最高的点
解决: 结构体+sort
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<cstdio>
using namespace std;
struct Node {
int x;
int y;
int z;
}node[50001];
int cmp(Node a, Node b)
{
return a.z < b.z;
}
int main()
{
int n;
cin >> n;
double sum = 0;
for (int i = 0; i < n; i++)
{
cin >> node[i].x >> node[i].y >> node[i].z;
}
sort(node, node + n,cmp);
for (int i = 0; i < n - 1; i++)
sum += sqrt(double((node[i + 1].x - node[i].x) * (node[i + 1].x - node[i].x) + (node[i + 1].y - node[i].y) * (node[i + 1].y - node[i].y) + (node[i + 1].z - node[i].z) * (node[i + 1].z - node[i].z)));
printf("%.3lf", sum);
return 0;
}
4:P1605
迷宫有多少种从起点到终点方案
解决:dfs
代码:
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int n, m, t;
int sx, sy, fx, fy;
int arr[6][6];
int visit[6][6];
int sum;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { -1,1,0,0 };
void walk(int x,int y)
{
if (x == fx && y == fy)
{
sum++;
return;
}
else
{
for (int i = 0; i <= 3; i++)
{
if (visit[x + dx[i]][y + dy[i]] == 0 && arr[x + dx[i]][y + dy[i]] == 1)
{
visit[x][y] = 1;
walk(x + dx[i], y + dy[i]);
visit[x][y] = 0;
}
}
}
}
int main()
{
cin >> n >> m >> t;
cin >> sx >> sy >> fx >> fy;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
arr[i][j] = 1;
for (int i = 1; i <= t; i++)
{
int a, b;
cin >> a >> b;
arr[a][b] = 0;
}
walk(sx, sy);
cout << sum;
return 0;
}
5:P1101
给一 n \times nn×n 的字母方阵,内可能蕴含多个 yizhong
单词。单词在方阵中是沿着同一方向连续摆放的。摆放可沿着 88 个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间可以交叉,因此有可能共用字母。输出时,将不是单词的字母用 *
代替,以突出显示单词
解决:dfs(搜索染色)
代码:
#include<iostream>
#include<cstdio>
#include<string>
int count1;
using namespace std;
char arr[110][110];
int visit[110][110];
int dx[9] = { 0,0,1,-1,1,1,-1,-1 };
int dy[9] = { 1,-1,0,0,1,-1,1,-1};
int brr[10001][5];
char k[9] = " yizhong";
bool search(int a,int b,int c,int d,int next)
{
if (next >= 8)
{
visit[a][b] = 1;
return 1;
}
else
{
if (arr[a + c][b + d] == k[next])
{
if (search(a + c, b + d, c, d, next+1))
{
visit[a][b] = 1;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> arr[i][j];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (arr[i][j] == 'y')
{
brr[++count1][0] = i;
brr[count1][1] = j;
}
}
}
while (count1)
{
int i = brr[count1][0];
int j = brr[count1][1];
for (int x = 0; x <= 8; x++)
{
if (arr[i + dx[x]][j + dy[x]] == 'i')
if(search(i + dx[x], j + dy[x], dx[x], dy[x], 3))
visit[i][j] = 1;
}
count1--;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (visit[i][j] == 1) cout << arr[i][j];
else cout << '*';
}
cout << endl;
}
return 0;
}
6:P1160
队列安排
解决:链式结构
代码:
#include<iostream>
using namespace std;
int arr[100010][4];
int main()
{
int n;
cin >> n;
int j = 1;
arr[1][1] = 1;
for (int i = 2; i <= n; i++)
{
arr[i][1] = i;
int a, b;
cin >> a >> b;
if (b == 0)
{
arr[arr[a][3]][2] = arr[i][1];
arr[i][3] = arr[a][3];
arr[a][3] = arr[i][1];
arr[i][2] = arr[a][1];
if (a == j) j = i;
}
else
{
arr[arr[a][2]][3] = arr[i][1];
arr[i][2] = arr[a][2];
arr[a][2] = arr[i][1];
arr[i][3] = arr[a][1];
}
}
int m;
cin >> m;
for (int i = 1; i <= m; i++)
{
int x;
cin >> x;
if (arr[x][1])
{
arr[x][1] = 0;
arr[arr[x][3]][2] = arr[arr[x][2]][1];
arr[arr[x][2]][3] = arr[arr[x][3]][1];
n--;
if (x == j) j = arr[x][2];
}
}
int i = 1;
int x = j;
while (i <= n)
{
cout << arr[x][1]<<" ";
x = arr[x][2];
i++;
}
return 0;
}
7:P1540
机器翻译
解决:vector数组
代码:#include<iostream>
#include<vector>
using namespace std;
vector<int>arr;
int main()
{
int n, m, sum = 0,fg=0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x;
cin >> x;
int y = 0;
if (fg == n)
{
for (int j = 0; j < n; j++)
{
if (arr[j] == x)
{
y = 1;
break;
}
}
if (y == 0)
{
arr.erase(arr.begin() + 0, arr.begin() + 1);
arr.push_back(x);
sum++;
}
}
else
{
for (int j = 0; j < fg; j++)
{
if (arr[j] == x)
{
y = 1;
break;
}
}
if (y == 0)
{
arr.push_back(x);
sum++;
fg++;
}
}
}
cout << sum;
return 0;
}
8:P1305
给一个二叉树,求其前序遍历
解决:vector<int>arr[max]
代码:#include<iostream>
#include<vector>
using namespace std;
vector<char>arr[110];
void dfs(char root)
{
cout << root;
if (arr[root][0] != '*')
dfs(arr[root][0]);
if (arr[root][1] != '*')
dfs(arr[root][1]);
}
int main()
{
int n;
cin >> n;
char root;
for (int i = 1; i <= n; i++)
{
char a, b, c;
cin >> a >> b >> c;
if (i == 1) root = a;
arr[a].push_back(b);
arr[a].push_back(c);
}
dfs(root);
}
9: P1827
知道中序遍历,和前序遍历,求后序遍历
解决:找根点,然后一直分
代码:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void home(string a, string b)
{
if (a.empty()) return;
char root = a[0];
int k = b.find(root);
a.erase(a.begin());
string a1 = a.substr(0, k );
string a2 = a.substr(k);
string b1 = b.substr(0,k);
string b2 = b.substr(k+1);
home(a1, b1);
home(a2, b2);
cout << root;
}
int main()
{
string arr;
string brr;
cin >> arr >> brr;
home(brr, arr);
return 0;
}
10:P1241
括号序列
解决:用栈
代码:
#include<iostream>
#include<string>
using namespace std;
char brr[110];
char crr[110];
char drr[110];
int main()
{
string arr;
cin >> arr;
int top = 0;
int n = arr.length();
for (int i = 0; i < n; i++)
{
if (arr[i] == '(' || arr[i] == '[')
{
brr[++top] = arr[i];
crr[top] = i;
if (arr[i] == '(') drr[i] = ')';
else drr[i] = ']';
}
if (arr[i] == ')')
{
if (top&&brr[top] == '(')
{
drr[crr[top]] = ' ';
top--;
}
else
drr[i] = '(';
}
if (arr[i] == ']')
{
if (top&&brr[top] == '[')
{
drr[crr[top]] =' ';
top--;
}
else drr[i] = '[';
}
}
for (int i = 0; i < n; i++)
{
if (drr[i] == '(' || drr[i] == '[') cout << drr[i] << arr[i];
else if (drr[i] == ')' || drr[i] == ']') cout << arr[i] << drr[i];
else cout << arr[i];
}
return 0;
}
11:P1229
知道前序和后序,求中序可能个数
找AB与BA 然后2^
代码:#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int sum = 0;
string arr;
string brr;
cin >> arr >> brr;
char x = arr[0];
for (int a = 0; a < arr.length(); a++)
for (int b = 1; b < brr.length(); b++)
if (arr[a] == brr[b] && arr[a + 1] == brr[b - 1])
sum++;
cout<<2*sum;
return 0;
}