https://ac.nowcoder.com/acm/contest/32708
1.King of Gamers
0
0
\frac{0}{0}
00每次赢分子分母+1,输分母+1,当前值>
a
b
\frac{a}{b}
ba必输,否则必赢。
设赢了
x
x
x次,上述易知最后分母为
n
n
n,则
x
n
>
a
b
\frac{x}{n}>\frac{a}{b}
nx>ba,即
x
>
a
∗
n
b
x>\frac{a*n}{b}
x>ba∗n。细节特判最后答案合不合法+1即可。
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
ll n,a,b;
cin>>n>>a>>b;
ll ans=n*a/b;
if(ans*b<=a*(n-1))ans++;
cout<<ans<<'\n';
}
}
2.Divisions
打表找规律注意0
#include <bits/stdc++.h>
using namespace std;
int fan(vector<int>a){
int ans=0;
for(int i=0;i<(1<<a.size());i++){
vector<int>u,v;
for(int j=0;j<a.size();j++){
if(i>>j&1)u.push_back(a[j]);
else v.push_back(a[j]);
}
reverse(v.begin(),v.end());
ans+=is_sorted(u.begin(),u.end())&&is_sorted(v.begin(),v.end());
}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
if(n==0){
cout<<"4\n50 100 1 25";
}else if(n==1){
cout<<"6\n1 1 4 5 1 4";
}else{
vector<int>res;
int la=1,ans=1,cnt=0;
while(ans<n){
if(ans+(1ll<<cnt)<=n){
ans+=(1ll<<cnt);
cnt++;
res.push_back(la);
}else{
la++;
cnt=0;
}
}
cout<<res.size()<<'\n';
for(auto &t:res){
cout<<t<<" ";
}
}
}
3.Find the Maximum
非常经典的二分答案。
∑
u
∈
V
(
−
x
2
+
b
u
x
)
∣
V
∣
=
−
x
2
+
∑
u
∈
V
b
u
∣
V
∣
x
\frac{\sum_{u\in{V}}(-x^2+b_ux)}{|V|}=-x^2+\frac{\sum_{u\in V}b_u}{|V|}x
∣V∣∑u∈V(−x2+bux)=−x2+∣V∣∑u∈Vbux
令
r
e
s
=
∑
u
∈
V
b
u
∣
V
∣
res=\frac{\sum_{u\in V}b_u}{|V|}
res=∣V∣∑u∈Vbu由基本不等式易知答案为
r
e
s
2
4
\frac{res^2}{4}
4res2
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5+10 ;
constexpr double inf = 1e14 ;
double val[N];
double dp[N],ans[N];
vector<int>g[N];
int fa[N];
template<class Tp=int>
Tp read(){
char ch=getchar();bool f=1;Tp x=0;
while(!isdigit(ch)){
if(ch=='-')f=false;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return f?x:-x;
}
template<class Tp=int>
void print(Tp n){
if(n>9)print(n/10);
putchar(n%10|'0');
}
vector<int>uu;
void dfs(int x,int f){
fa[x]=f;
for(auto &t:g[x]){
if(t==f)continue;
dfs(t,x);
}
uu.push_back(x);
}
bool check(double now){
for(auto &t:uu){
dp[t]=val[t]-now;
ans[t]=-1e100;
for(auto &x:g[t])if(x!=fa[t]){
ans[t]=max(ans[t],dp[x]+dp[t]);
dp[t]=max(dp[t],dp[x]+val[t]-now);
ans[t]=max(ans[t],ans[x]);
}
}
return ans[1]>=0;
}
int main()
{
int n=read();
for(int i=1;i<=n;i++){
val[i]=read();
}
for(int i=1,x,y;i<n;i++){
x=read(),y=read();
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,0);
double ans;
double l=-1e5,r=1e5;
for(int i=1;i<=50;i++){
double mid=(l+r)/2.0;
if(check(mid))l=mid;
else r=mid;
}
ans=l;
for(int i=1;i<=n;i++)val[i]=-val[i];
l=-1e5,r=1e5;
for(int i=1;i<=50;i++){
double mid=(l+r)/2.0;
if(check(mid))l=mid;
else r=mid;
}
ans=max(ans,l);
cout<<fixed<<setprecision(6);
cout<<ans*ans/4.0;
}
持续更新中…