ABC373 A - September
代码(签到题)
#include <cstdio>
#include <cstring>
using namespace std;
char s[1111];
int ans;
int main(){
for (int i=1;i<=12;++i){
scanf("%s",s+1);
if (strlen(s+1)==i) ++ans;
}
printf("%d",ans);
return 0;
}
ABC373 B - 1D Keyboard
代码(签到题)
#include <cstdio>
#include <cstring>
using namespace std;
char s[1111];
int ans,lst,pos[11111];
int Abs(int x){return x<0?-x:x;}
int main(){
scanf("%s",s+1);
for (int i=1;i<=26;++i){
pos[s[i]-64]=i;
}
for (int i=2;i<=26;++i){
ans+=Abs(pos[i]-pos[i-1]);
}
printf("%d",ans);
return 0;
}
ABC373 C - Max Ai+Bj
代码(签到题)
#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n,mx1=-2e9,mx2=-2e9;
cin>>n;
for (int i=1,x;i<=n;++i) cin>>x,mx1=max(mx1,x);
for (int i=1,x;i<=n;++i) cin>>x,mx2=max(mx2,x);
cout<<mx1+mx2;
return 0;
}
ABC373 D - Hidden Weights
分析
对于所有弱连通分支,任取一种生成树令根节点点权为 \(0\),
按照边的要求扩展,最后每条边都判断是否满足有向的要求
代码
#include <iostream>
using namespace std;
const int N=200011;
struct node{int y,w,next;}e[N<<1];
int v[N],as[N],n,m,et=1; long long dis[N];
void dfs(int x){
v[x]=1;
for (int i=as[x];i;i=e[i].next)
if (!v[e[i].y]){
if (i&1) dis[e[i].y]=dis[x]-e[i].w;
else dis[e[i].y]=dis[x]+e[i].w;
dfs(e[i].y);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for (int i=1;i<=m;++i){
int x,y,w; cin>>x>>y>>w;
e[++et]=(node){y,w,as[x]},as[x]=et;
e[++et]=(node){x,w,as[y]},as[y]=et;
}
for (int i=1;i<=n;++i) if (!v[i]) dfs(i);
for (int i=1;i<=n;++i) cout<<dis[i]<<' ';
return 0;
}
ABC373 E - How to Win the Election
分析
二分答案,不然很难弄,二分答案之后就能定下某个人的得票数,如果原本这个人就是前 \(m\) 名,就要保证前 \(m+1\) 名其他人即使用了其它票都无法都比它高;
如果不是就是保证前 \(m\) 名,找出本来就比新票数还要高的人,剩下的人尝试都凑到得票数加一。
代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N=200011;
typedef long long lll;
lll a[N],k,s[N],b[N]; int n,m,rk[N],Rk[N];
bool cmp(int x,int y){return a[x]>a[y];}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for (int i=1;i<=n;++i) cin>>a[i],rk[i]=i,k-=a[i];
if (n==m){
for (int i=1;i<=n;++i) cout<<"0 ";
return 0;
}
sort(rk+1,rk+1+n,cmp);
for (int i=1;i<=n;++i) s[i]=s[i-1]+a[rk[i]],b[i]=a[rk[n-i+1]],Rk[rk[i]]=i;
for (int i=1;i<=n;++i){
lll l=0,r=k+1;
while (l<r){
lll mid=(l+r)>>1;
int pos=upper_bound(b+1,b+1+n,a[i]+mid)-b;
if (pos<=n-m+1) l=mid+1;
else{
pos=n-pos+1;
if (Rk[i]<=m){
if (k-mid<(a[i]+mid+1)*(m-pos)-(s[m+1]-s[pos]-a[i])) r=mid;
else l=mid+1;
}else{
if (k-mid<(a[i]+mid+1)*(m-pos)-(s[m]-s[pos])) r=mid;
else l=mid+1;
}
}
}
if (l==k+1) cout<<-1<<' ';
else cout<<l<<' ';
}
return 0;
}
ABC373 F - Knapsack with Diminishing Values
分析
非常好的背包,让我的大脑旋转,对于同体积的物品,由于价值与选择个数有关,不妨使用堆维护 \(\Delta k_iv_i-k_i^2\),每次选择最大的。
那么对于一个体积能够提供它与它倍数的新物品,一共有 \(W\log W\) 个,用这些新物品做完全背包即可,要注意出自原体积的物品继承的dp数组要来自上一体积而不是上一物品。
代码
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int N=3011;
typedef long long lll;
struct rec{
lll delta,w,cnt;
bool operator <(const rec &t)const{
return delta>t.delta;
}
};
lll n,m,f[N],g[N],dp[N]; multiset<rec>K[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for (int i=1;i<=n;++i){
lll w,v; cin>>w>>v;
K[w].insert((rec){v-1,v,1});
}
for (int i=1;i<=m;++i)
if (!K[i].empty()){
for (int j=0;j<=m;++j) f[j]=dp[j];
for (int j=i;j<=m;j+=i){
rec t=*K[i].begin();
if (t.delta<0) break;
K[i].erase(K[i].begin());
g[j]=g[j-i]+t.delta;
t.delta=t.w-1-2*(t.cnt++);
K[i].insert(t);
for (int k=m;k>=j;--k)
dp[k]=max(dp[k],f[k-j]+g[j]);
}
}
cout<<dp[m];
return 0;
}
ABC373 G - No Cross Matching
分析
KM 或者费用流,原题来自 UVA1411 Ants
代码
#include <cstdio>
#include <cctype>
#include <cmath>
#include <queue>
using namespace std;
const int N=311; bool vx[N],vy[N];
typedef double db; queue<int>q;
db slack[N],lx[N],ly[N],G[N][N];
int px[N],py[N],link[N],n,x[N],y[N];
int iut(){
int ans=0,f=1; char c=getchar();
while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans*f;
}
void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
db min(db a,db b){return a<b?a:b;}
db max(db a,db b){return a>b?a:b;}
void adjust(int y){
for (int _y;y;y=_y){
_y=px[link[y]];
px[link[y]]=y;
py[y]=link[y];
}
}
void bfs(int st){
for (int i=1;i<=n;++i) slack[i]=1e12,vx[i]=vy[i]=0;
while (!q.empty()) q.pop();
q.push(st);
while (1){
while (!q.empty()){
int x=q.front();
vx[x]=1,q.pop();
for (int y=1;y<=n;++y)
if (!vy[y]&&slack[y]>lx[x]+ly[y]-G[x][y]){
slack[y]=lx[x]+ly[y]-G[x][y],link[y]=x;
if (!slack[y]){
vy[y]=1;
if (!py[y]) {adjust(y); return;}
else q.push(py[y]);
}
}
}
db mn=1e12;
for (int i=1;i<=n;++i)
if (!vy[i]) mn=min(mn,slack[i]);
for (int i=1;i<=n;++i){
if (vx[i]) lx[i]-=mn;
if (vy[i]) ly[i]+=mn;
else slack[i]-=mn;
}
for (int i=1;i<=n;++i)
if (!vy[i]&&!slack[i]){
vy[i]=1;
if (!py[i]) {adjust(i); return;}
else q.push(py[i]);
}
}
}
void KM(){
for (int i=1;i<=n;++i){
link[i]=ly[i]=px[i]=py[i]=0,lx[i]=-1e12;
for (int j=1;j<=n;++j)
lx[i]=max(lx[i],G[i][j]);
}
for (int i=1;i<=n;++i) bfs(i);
}
int o(int x){return x*x;}
int main(){
n=iut();
for (int i=1;i<=n;++i) x[i]=iut(),y[i]=iut();
for (int j=1;j<=n;++j){
int X=iut(),Y=iut();
for (int i=1;i<=n;++i)
G[i][j]=-sqrt(o(X-x[i])+o(Y-y[i]));
}
KM();
for (int i=1;i<=n;++i)
print(px[i]),putchar(32);
return 0;
}
标签:AtCoder,Beginner,int,namespace,ans,using,373,include,ABC373 From: https://www.cnblogs.com/Spare-No-Effort/p/18450624