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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Codeforces 912 D. Fishes (贪心、bfs)

發布時間:2023/10/11 综合教程 99 老码农
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 912 D. Fishes (贪心、bfs) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:Fishes

題意:

  有一個n×m的魚塘,有一張r×r的漁網,現在往池塘里面放k條魚(每個格子只能放一條魚), 現在撒網的地方是隨機的(必須在池塘內),問能捕的魚的期望值最大是多少?

題解:

  這題dfs我是真的沒想到。。因為怎么說,總是感覺這樣有些暴力吧@。@# 要好好反思了。這題首先要把每個位置網覆蓋的次數的公式推出來(用if else也行其實),因為可以發現最中間的位置一定最大,所以選取最中間的位置開始bfs,把遇到的點都放到優先隊列中,這里對優先隊列進行符號重載就可以很好地解決排序的問題,很值得學習。

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+;
long long N,M,r,k;
struct P
{
long long first,second;
P(int x,int y){first = x,second = y;}
};
priority_queue <P> que;
set<int> st[MAX_N];
long long get_val(P t)
{
return (min(N - r + , t.first) - max(1ll, t.first - r + ) + ) * (min(M - r + , t.second) - max(1ll, t.second - r + ) + );
}
bool operator < (const P &a,const P &b)
{
return get_val(b) > get_val(a);
} int main()
{
while(cin>>N>>M>>r>>k)
{
while(!que.empty()) que.pop();
for(int i=;i<MAX_N;i++) st[i].clear();
int x = (N+)/;
int y = (M+)/;
que.push(P(x,y));
st[x].insert(y);
double ans = ;
long long num = ;
while(!que.empty())
{
P t = que.top();que.pop();
ans += (get_val(t)*1.0)/((N-r+)*(M-r+)*1.0);
k--;
if(!k) break;
//cout<<t.first<<"..."<<t.second<<"...."<<get_val(t)<<endl;
if(t.first+> && t.first+<=N && st[t.first+].count(t.second) == ) que.push(P(t.first+,t.second)),st[t.first+].insert(t.second);
if(t.first-> && t.first-<=N && st[t.first-].count(t.second) == ) que.push(P(t.first-,t.second)),st[t.first-].insert(t.second);
if(t.second- > && t.second- <=M && st[t.first].count(t.second-) == ) que.push(P(t.first,t.second-)),st[t.first].insert(t.second-);
if(t.second+ > && t.second+ <=M && st[t.first].count(t.second+) == ) que.push(P(t.first,t.second+)),st[t.first].insert(t.second+);
}
printf("%.10lf\n",ans);
}
return ;
}

總結

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

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