最後40%速度卡住,不得不說這時間複雜度其實真的不怎麼樂觀,但我也想不出更快的了
我的想法是不停的掃,一發現有棵樹有足夠空間倒下,就用link list跨過那棵樹,當找不到樹可砍時found = false,跳出while迴圈
有沒有大神有更好的做法...
#include#define ll long longusing namespace std;ll cnt = 0, highest = 0, pos[100005], hight[100005],edge[100005][2], n, l;bool cut[100005] = {false};int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> l;for (int i = 1; i <= n; ++i){cin >> pos[i];}pos[0] = 0;pos[n + 1] = l;for (int i = 1; i <= n; ++i){cin >> hight[i];}edge[0][1] = 1;edge[n + 1][0] = n;for (int i = 1; i <= n; ++i){edge[i][0] = i - 1;edge[i][1] = i + 1;}while (true){bool found = false;for (int i = 1; i <= n; ++i){if (!cut[i]){if ((pos[i] - hight[i] >= pos[edge[i][0]]) || (pos[i] + hight[i] <= pos[edge[i][1]])){++cnt;edge[edge[i][0]][1] = edge[i][1];edge[edge[i][1]][0] = edge[i][0];highest = max(highest, hight[i]);cut[i] = true;found = true;}}}if (!found)break;}cout << cnt << endl << highest;return 0;}
改了很久終於AC了......分享一下我的作法:
1. 不需要不停的掃,只要從最左邊向右檢查,如果有空間倒下,再往回檢查左邊那棵樹
2. for迴圈不要用++i,這裡不改你的程式一樣40% TLE,改成i=edge[i][1]會快很多(我想超久才把這裡改掉)
最後40%速度卡住,不得不說這時間複雜度其實真的不怎麼樂觀,但我也想不出更快的了
我的想法是不停的掃,一發現有棵樹有足夠空間倒下,就用link list跨過那棵樹,當找不到樹可砍時found = false,跳出while迴圈
有沒有大神有更好的做法...
#include#define ll long longusing namespace std;ll cnt = 0, highest = 0, pos[100005], hight[100005],edge[100005][2], n, l;bool cut[100005] = {false};int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> l;for (int i = 1; i <= n; ++i){cin >> pos[i];}pos[0] = 0;pos[n + 1] = l;for (int i = 1; i <= n; ++i){cin >> hight[i];}edge[0][1] = 1;edge[n + 1][0] = n;for (int i = 1; i <= n; ++i){edge[i][0] = i - 1;edge[i][1] = i + 1;}while (true){bool found = false;for (int i = 1; i <= n; ++i){if (!cut[i]){if ((pos[i] - hight[i] >= pos[edge[i][0]]) || (pos[i] + hight[i] <= pos[edge[i][1]])){++cnt;edge[edge[i][0]][1] = edge[i][1];edge[edge[i][1]][0] = edge[i][0];highest = max(highest, hight[i]);cut[i] = true;found = true;}}}if (!found)break;}cout << cnt << endl << highest;return 0;}
改了很久終於AC了......分享一下我的作法:
1. 不需要不停的掃,只要從最左邊向右檢查,如果有空間倒下,再往回檢查左邊那棵樹2. for迴圈不要用++i,這裡不改你的程式一樣40% TLE,改成i=edge[i][1]會快很多(我想超久才把這裡改掉)
已AC,超級感謝