本文主要是介绍(CodeForce) Codeforces Round #541 (Div. 2) A,B,C,D,F,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
A. Sea Battle
:围成的不规则图像的周长(就是矩形的周长),在加四个角重复的部分。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
ll w1,h1,w2,h2;
int main(){std::ios::sync_with_stdio(0);cin>>w1>>h1>>w2>>h2;ll sum=(h1+h2)*2+(max(w1,w2))*2+4;cout<<sum<<endl;return 0;
}
B. Draw!
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=1e4+50;
ll a[maxn],b[maxn],le,ri;
int main() {std::ios::sync_with_stdio(0);int n;ll ans=0,tdd=-1;cin>>n;for(int i=1; i<=n; ++i) {cin>>a[i]>>b[i];}for(int i=1; i<=n; ++i) {le=max(a[i-1],b[i-1]);ri=min(a[i],b[i]);if(ri>=le) ans+=ri-max(le-1,tdd),tdd=max(le,ri);}cout<<ans<<endl;return 0;
}
C. Birthday
:只要序列高度(大小)是一个山字形就行了,所以排个序,操作一下就行
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=1005;
int n;
int a[maxn];
vector<int> ans;
int main() {std::ios::sync_with_stdio(0);cin>>n;for(int i=0; i<n; ++i) cin>>a[i];sort(a,a+n);for(int i=n-1; i>=0; --i) {if(i&1) ans.insert(ans.begin(),a[i]);else ans.insert(ans.end(),a[i]);}for(int i=0; i<(int)ans.size(); ++i) {cout<<ans[i]<<" ";}cout<<endl;return 0;
}
D. Gourmet choice
单独写了戳我
F. Asya And Kittens
:这也是一个并查集的题目,我们只需将每次合并的东西维护一下,这题就迎刃而解了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=155000;
int fa[maxn],rk[maxn]; //这里的rk是 以i为父亲为集合的元素
vector<int> G[maxn];
int find(int x) {if(fa[x]==x) return fa[x];else return fa[x]=find(fa[x]);
}
void unity(int x,int y) {x=find(x),y=find(y);if(x==y) return;if(rk[x]<rk[y]) swap(x,y);fa[y]=x,rk[x]+=rk[y];for(int i=0; i<=rk[y]-1; ++i) { //每次合并我们把集合的元素也并一下G[x].push_back(G[y][i]);}
}
int n;
int main() {std::ios::sync_with_stdio(0);cin>>n;for(int i=1; i<=n; ++i)fa[i]=i,G[i].push_back(i),rk[i]=1;int x,y;for(int i=1; i<=n-1; ++i) {cin>>x>>y;unity(x,y);}int st=find(1);for(int i=0; i<=n-2; ++i) {cout<<G[st][i]<<' ';}cout<<G[st][n-1]<<endl;return 0;
}
这篇关于(CodeForce) Codeforces Round #541 (Div. 2) A,B,C,D,F的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!