日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces1388 D. Captain Flint and Treasure(贪心)

發布時間:2023/12/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces1388 D. Captain Flint and Treasure(贪心) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure’s location or may not. That’s why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this…

There are two arrays 𝑎 and 𝑏 of length 𝑛. Initially, an 𝑎𝑛𝑠 is equal to 0 and the following operation is defined:

Choose position 𝑖 (1≤𝑖≤𝑛);
Add 𝑎𝑖 to 𝑎𝑛𝑠;
If 𝑏𝑖≠?1 then add 𝑎𝑖 to 𝑎𝑏𝑖.
What is the maximum 𝑎𝑛𝑠 you can get by performing the operation on each 𝑖 (1≤𝑖≤𝑛) exactly once?

Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

Input
The first line contains the integer 𝑛 (1≤𝑛≤2?105) — the length of arrays 𝑎 and 𝑏.

The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (?106≤𝑎𝑖≤106).

The third line contains 𝑛 integers 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤𝑛 or 𝑏𝑖=?1).

Additional constraint: it’s guaranteed that for any 𝑖 (1≤𝑖≤𝑛) the sequence 𝑏𝑖,𝑏𝑏𝑖,𝑏𝑏𝑏𝑖,… is not cyclic, in other words it will always end with ?1.

Output
In the first line, print the maximum 𝑎𝑛𝑠 you can get.

In the second line, print the order of operations: 𝑛 different integers 𝑝1,𝑝2,…,𝑝𝑛 (1≤𝑝𝑖≤𝑛). The 𝑝𝑖 is the position which should be chosen at the 𝑖-th step. If there are multiple orders, print any of them.

Examples
inputCopy
3
1 2 3
2 3 -1
outputCopy
10
1 2 3
inputCopy
2
-1 100
2 -1
outputCopy
99
2 1
inputCopy
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
outputCopy
-9
3 5 6 1 9 4 10 7 8 2

題意:
每次操作你可以選擇iii,并且獲得a[i]a[i]a[i]的值。同時如果b[i]!=?1b[i]!=-1b[i]!=?1a[b[i]]a[b[i]]a[b[i]]加上a[i]a[i]a[i]。每個iii只能選擇一次,問最大能獲得多大的值。
保證i,b[i],b[b[i]]...i,b[i],b[b[i]]...i,b[i],b[b[i]]...不是環。

思路:
因為i,b[i],b[b[i]]...i,b[i],b[b[i]]...i,b[i],b[b[i]]...不是環,所以可以iiib[i]b[i]b[i]連邊,則構成了一棵樹。對于所有b[i]=?1,ib[i]=-1,ib[i]=?1,i為該樹根節點。

因為每次選擇完iii會對b[i]b[i]b[i]有影響,所以我們肯定希望影響是正面的。也就是如果a[i]a[i]a[i]是正數,毫無疑問得算上這個影響。否則就把iii放到后面選擇。

這樣貪心算法就出來了,遞歸的考慮,考慮所有子樹的選擇,如果a[v]a[v]a[v]大于0,那么就早于xxx選擇,否則后于xxx選擇。并且a[x]a[x]a[x]加上所有大于0的a[v]a[v]a[v]。

路徑等同于dfs的順序,當子節點的a[v]a[v]a[v]為負數時,則先輸出xxx,再輸出子樹v的節點。

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <iostream> #include <map> #include <string> #include <set> #include <deque> typedef long long ll; using namespace std;const int maxn = 2e5 + 7; const ll INF = 0x3f3f3f3f3f3f3f3f; ll a[maxn],b[maxn]; vector<int>G[maxn]; ll f[maxn]; deque<int>res;void DP(int x) {f[x] = a[x];ll sum = 0;for(int i = 0;i < G[x].size();i++) {int v = G[x][i];DP(v);f[x] += f[v];if(a[v] >= 0) sum += a[v];}a[x] += sum;f[x] += sum; }int cmp(int x,int y) {return a[x] > a[y]; }void dfs2(int x) {sort(G[x].begin(),G[x].end(),cmp);int flag = 0;for(int i = 0;i < G[x].size();i++) {int v = G[x][i];if(a[v] < 0 && flag == 0) {res.push_back(x);flag = 1;}dfs2(v);}if(!flag) res.push_back(x); }int main() {int n;scanf("%d",&n);for(int i = 1;i <= n;i++) {scanf("%lld",&a[i]);}for(int i = 1;i <= n;i++) {scanf("%lld",&b[i]);}ll ans = 0;for(int i = 1;i <= n;i++) {if(b[i] != -1) {G[b[i]].push_back(i);}}for(int i = 1;i <= n;i++) {if(b[i] == -1) {DP(i);ans += f[i];}}for(int i = 1;i <= n;i++) {if(b[i] == -1) {dfs2(i);}}printf("%lld\n",ans);while(!res.empty()) {printf("%d ",res.front());res.pop_front();}return 0; }

總結

以上是生活随笔為你收集整理的Codeforces1388 D. Captain Flint and Treasure(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。