日韩性视频-久久久蜜桃-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(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲1页 | 操操操操操操操 | 日本少妇吞精囗交 | 精品影院 | 桃色在线视频 | 国产乱码一区二区三区 | 国产夫妻性爱视频 | 日韩插插插 | 亚洲色图35p | 成人久久久久久久 | 欧美午夜一区二区三区 | 黑名单上的人全集免费观看 | 韩国黄色av| 久久影视大全 | 精品久久人妻av中文字幕 | 性欧美18一19内谢 | 国产日韩欧美在线观看视频 | 风韵少妇性饥渴推油按摩视频 | 免费人成网站 | 色婷婷国产精品综合在线观看 | 在线视频午夜 | 手机亚洲第一页 | 乱色精品无码一区二区国产盗 | 国产精品久久久久久影视 | 国产又粗又猛又爽又黄91 | 伊人av一区 | 免费看60分钟黄视频 | 热99在线 | 国产一区二区三区四区五区六区 | 无码国产伦一区二区三区视频 | 激情伊人 | 色诱av手机版 | av网站在线免费播放 | 精品一区二区久久久久蜜桃 | 亚洲成年人网站在线观看 | 浮力影院草草 | 插入综合网 | 尤果网福利视频在线观看 | av中文字幕一区二区 | 国产精品成人av性教育 | 成人aaa视频| 不用播放器av | 97人人插| 日本成人黄色片 | 欧美成人精品欧美一级乱黄 | 色欲一区二区三区精品a片 在线观看黄网站 | 视色在线 | 久久久国产亚洲 | 成人在线观看小视频 | 国内激情自拍 | 国产黄a三级三级看三级 | 久久国语精品 | 噜噜狠狠狠狠综合久久 | aaaa免费视频 | 国产超碰人人爽人人做人人爱 | 已满十八岁免费观看 | 国产一区二区三区影院 | 99国产成人精品 | 国产a√| 国产精品电影网站 | 日韩欧美的一区二区 | 欧美成人三区 | 午夜无遮挡 | 久久91亚洲精品中文字幕奶水 | 日韩一区欧美一区 | 日本大尺度做爰呻吟 | 阿娇全套94张未删图久久 | 一级片成人 | 五月精品 | 中文字幕在线观看日本 | 国产精品夜夜躁视频 | 久久国产精品久久久 | 四虎精品成人免费网站 | 中文字幕第8页 | 欧美又粗又大xxxxbbbb疯狂 | 99国产精品一区二区 | 秋霞一级全黄大片 | 亚洲天堂岛 | 一级黄色大片 | 精品视频在线一区 | 麻豆精品国产 | xxxxx黄色 | 亚洲欧美一区二区三区在线观看 | 久免费一级suv好看的国产 | 天堂视频免费看 | 糖心logo在线观看 | 欧美三级小视频 | 国产香蕉在线 | 国产对白视频 | 不卡的av电影 | 欧美激情亚洲综合 | 免费观看黄一级视频 | 武林美妇肉伦娇喘呻吟 | 黄色免费一级视频 | 亚洲最新在线视频 | 精品国产成人av | 午夜国产一区二区三区 | 91亚洲欧美激情 | 一道本久久|