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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

牛客网 小白赛4 A三角形【贪心】

發(fā)布時(shí)間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客网 小白赛4 A三角形【贪心】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【前驅(qū)】:在指定長(zhǎng)度的棍子中找到能組成最大周長(zhǎng)三角形的三根棍子
鏈接:https://www.nowcoder.com/acm/contest/134/A
來(lái)源:牛客網(wǎng)

題目描述
鐵子從森林里收集了n根木棍,她開始將它們按順序的排成一排,從左到右依次為1到n,她回想起
在數(shù)學(xué)課上老師教她的三角形知識(shí),她開始從這些木棍中間找三根木棍來(lái)組成一個(gè)周長(zhǎng)最大的三角形,
這時(shí)她的兄弟順溜偷偷的溜了過來(lái),偷走了第i根木棍,現(xiàn)在她想知道現(xiàn)在能夠組成周長(zhǎng)最大的三角形
的周長(zhǎng)是多少?
輸入描述:
第一行兩個(gè)整數(shù)n和q。(1 ≤ n, q ≤ 105)
第二行n個(gè)整數(shù)表示第i根木棍的長(zhǎng)度ai。(1 ≤ ai ≤ 109)
接下來(lái)q行,每行一個(gè)整數(shù)表示被順溜偷走的木棍編號(hào)。注意每行的事件是獨(dú)立的,也就是說(shuō)每一次操作都是對(duì)于原來(lái)的n根木棍進(jìn)行的。

輸出描述:
對(duì)于每個(gè)詢問輸出一行表示答案,如果刪除木棍后無(wú)法組成三角形則輸出 -1 。
示例1
輸入
復(fù)制
6 2
1 2 3 4 5 6
6
5
輸出
復(fù)制
12
13
【代碼】:

#include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include<list> #include<assert.h> #include<bitset> #include<numeric> using namespace std;typedef long long ll; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const ll LNF = 1e18; const int maxn = 1e5 + 100; const int maxm = 100; const double PI = acos(-1.0); const double eps = 1e-8; //const int dx[] = {-1,1,0,0,1,1,-1,-1}; //const int dy[] = {0,0,1,-1,1,-1,1,-1}; const int dx[] = {-1,1,0,0}; const int dy[] = {0,0,1,-1};const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };ll n,q,p; struct node {ll x,id; }a[maxn];bool cmp(node a, node b) {return a.x>b.x; //從最大值開始枚舉 }ll solve() {for(ll i=1; i+2<=n; i++) //i+2最大值不能超過n{ll k1=i,k2=i+1,k3=i+2; //模擬指針if(a[i].id == p) { k1++; k2++; k3++;} //遇到要?jiǎng)h除的目標(biāo),指針后移else if(a[i+1].id == p) { k2++; k3++; }else if(a[i+2].id == p) { k3++; }if(a[k1].x < a[k2].x + a[k3].x) return a[k1].x + a[k2].x + a[k3].x; //合法返回周長(zhǎng)}return -1; } int main() {scanf("%lld%lld",&n,&q);for(ll i=1;i<=n;i++){a[i].id=i;scanf("%lld",&a[i].x);}sort(a+1,a+n+1,cmp);while(q--){scanf("%lld",&p);printf("%lld\n",solve());} }

轉(zhuǎn)載于:https://www.cnblogs.com/Roni-i/p/9193666.html

總結(jié)

以上是生活随笔為你收集整理的牛客网 小白赛4 A三角形【贪心】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。