题解分享
题解分享简介
修剪灌木(编程题) - 题解
对于 [1] 我们可以发现园丁这样走是4: (一个->代表一步)
```text
-> -> ->
1 2 3 (编号)
<- // 注意: [3]这个位置只算一次, 因为是原地向后转
```
为了方便计算, 我们可以把[1]头上的箭头放到 [3] 下面:
```text
-> ->
1 2 3 (编号)
<- <-
```
有感觉了吗? 下面是对于 1 ~ 4 按照上面的写法: 我们现在对于[3]讨论
情况1: 往右边 为 2
```text
->
1 2 3 4 (编号)
<-
```
情况2: 往左边 为 4
```text
<-<-
1 2 3 4 (编号)
->->
```
显然我们需要取大的, 即 max(i - 1, n - i) (此处i从1开始), 得到的是一次(即上边的<-<-), 故我们可以 2 即 max(i - 1, n - i) 2, 为最终答案
---
代码
```cpp
#include <cstdio>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
printf("%d\n", max(i - 1, n - i) * 2);
}
return 0;
}
```
查看全文
0
0
1
0
修剪灌木(编程题) - 题解
本题可以看出最高的高度其实与平常没有什么区别,因为爱丽丝如果走到第i棵树 ,那么她回来再次遇见这棵树的路程是相同的所以要2,然后我们只考虑了右端点,没有考虑左端点,所以要i-1;最后取max
include
using namespace std;
int main(void){
ios::sync\_with\_stdio(false),cout.tie(0),cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++)
{
```
cout<<max(n-i,i-1)*2<<endl;
```
}
return 0;
}
查看全文
0
0
0
1
修剪灌木(编程题) - 题解
模拟
```
#include<bits/stdc++.h>
#include<vector>
using namespace std;
const int N = 1e4+9;
int mxh[N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//取消同步流,让C++代码更快
int n;
cin>>n;
vector<int> h(n);//记录每棵树的高度
for(int d = 1;d <= n;d++)
{
for(int i = 0;i < h.size();i++)
{
h[i] += 1;
h[d - 1] = 0;
mxh[i] = max(mxh[i], h[i]);
}
}
//第一次调转方向
for(int d = n;d >=1;d--)
{
for(int i = 0;i < h.size();i++)
{
h[i] += 1;
h[d - 1] = 0;
mxh[i] = max(mxh[i], h[i]);
}
}
//第二次调转方向
for(int d = 1;d <= n;d++)
{
for(int i = 0;i < h.size();i++)
{
h[i] += 1;
h[d - 1] = 0;
mxh[i] = max(mxh[i], h[i]);
}
}
for(int i = 0;i < h.size();i++)
{
cout<< mxh[i] <<endl;
}
return 0;
}
```
查看全文
0
0
0
0
修剪灌木(编程题) - 题解
```
//打表
// #include <bits/stdc++.h>
// using namespace std;
// const int N = 1e4 + 10;
// int n, h[N], m[N];
// bool dx;
// void pass_day() {
// for (int i = 0; i < n; i ++) {
// h[i] ++;
// }
// }
// void caculate_max() {
// for (int i = 0; i < n; i ++) {
// m[i] = max(m[i], h[i]);
// }
// }
// void solve() {
// for (int i = 0; i < 9999; i ++) {
// for (int j = 0; j < n - 1; j ++) {
// pass_day();
// caculate_max();
// h[j] = 0;
// }
// for (int j = n - 1; j > 0; j --) {
// pass_day();
// caculate_max();
// h[j] = 0;
// }
// }
// cout << n << ": ";
// for (int i = 0; i < n; i ++) cout << m[i] << ' ';
// cout << endl;
// }
// int main(void) {
// for (n = 1; n <= 12; n ++) solve();
// return 0;
// }
/*
获得数据如下
1: 0
2: 2 2
3: 4 2 4
4: 6 4 4 6
5: 8 6 4 6 8
6: 10 8 6 6 8 10
7: 12 10 8 6 8 10 12
8: 14 12 10 8 8 10 12 14
9: 16 14 12 10 8 10 12 14 16
10: 18 16 14 12 10 10 12 14 16 18
11: 20 18 16 14 12 10 12 14 16 18 20
12: 22 20 18 16 14 12 12 14 16 18 20 22
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int n, h[N], idx;
int main(void) {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
if (n % 2) {
idx = n / 2 + 1;
h[idx] = n - 1;
for (int i = idx - 1; i >= 1; i --) {
h[i] = h[i + 1] + 2;
h[n - i + 1] = h[i];
}
} else {
idx = n / 2;
int initV = n;
for (int i = idx; i > 0; i --) {
h[i] = initV;
h[n - i + 1] = h[i];
initV += 2;
}
}
for (int i = 1; i <= n; i ++) cout << h[i] << endl;
return 0;
}
```
查看全文
0
0
0
0
修剪灌木(编程题) - 题解
```
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << max(n - i, i - 1) * 2;
cout << "\n";
}
return 0;
}
```
0
0
0
0
修剪灌木(编程题) - 题解
```
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x;
cin>>n;
```
for(int i=1;i<=n;i++)
{
x=max(i2-2,(n-i+1)2-2);
cout<<x<<endl;
}
return 0;
```
```
}
```
0
0
0
0



