0
点赞
收藏
分享

微信扫一扫

2018 计蒜之道 初赛 第一场 题解

_karen 2022-10-24 阅读 73


A. 百度无人车

签到

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int n;
ll a[MAXN];
ll check(ll m) {
ll t=0;
For(i,n) if(a[i]>m) t+=a[i]-m;
return t;
}
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
For(i,n){
a[i]=read();
}
ll p=read();
ll s;cin>>s;
s/=p;
ll l=1,r=20000,ans=20000;
while(l<=r){
ll m=(l+r)/2;
if(check(m)<=s) {ans=m; r=m-1;
}else l=m+1;
}
cout<<ans<<endl;
return 0;
}

百度科学家(困难)

主席树建图,SCC缩点
卡内存真好玩

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int n;
int a[MAXN];
int id[MAXN]={};
int idd[MAXN]={};
#undef
#define
ll sccans[MAXN]={};
vi G[MAXN],G2[MAXN];
vi S;
int vis[MAXN],sccno[MAXN],scc_cnt;
void dfs1(int u) {
if (vis[u]) return ;
vis[u] = 1;
int sz=SI(G[u]);
Rep(i,sz) dfs1(G[u][i]);
S.pb(u);
}
void dfs2(int u) {
if (sccno[u]) return;
sccno[u] = scc_cnt;
int sz=SI(G2[u]);
Rep(i,sz) dfs2(G2[u][i]);
}
void find_scc(int n) {
scc_cnt=0;
S.clear();
MEM(sccno) MEM(vis)
Rep(i,n) dfs1(i);
RepD(i,n-1)
if (!sccno[S[i]]) {
scc_cnt++; dfs2(S[i]);
}
}

int pp;
int tot=1;
int rot=1,lson[MAXN],rson[MAXN];
void build(int x,int l,int r) {
if(l==r){
id[l]=x ;return;
}
int m=(l+r)/2;
lson[x]=++tot;rson[x]=++tot;
G[x].pb(lson[x]);
G2[lson[x]].pb(x);
build(lson[x],l,m);

G[x].pb(rson[x]);
G2[rson[x]].pb(x);
build(rson[x],m+1,r);
}
void init(int &rot,int rot2,int l,int r, int p ,int newid) {
if(!rot) {
rot= ++tot;
}
if(l==r) {
id[newid]=rot;
return;
}
int m=(l+r)/2;
if(p<=m) {
lson[rot]=0;
rson[rot]=rson[rot2];
G[rot].pb(rson[rot]);
G2[rson[rot]].pb(rot);
init(lson[rot],lson[rot2],l,m,p,newid);
G[rot].pb(lson[rot]);
G2[lson[rot]].pb(rot);
}
else {
rson[rot]=0;
lson[rot]=lson[rot2];
G[rot].pb(lson[rot]);
G2[lson[rot]].pb(rot);
init(rson[rot],rson[rot2],m+1,r,p,newid);
G[rot].pb(rson[rot]);
G2[rson[rot]].pb(rot);
}
}
void get(int x,int l,int r,int L,int R) {
int m=(l+r)/2;
if(L<=l&&r<=R) {
G[id[idd[pp]]].pb(x);
G2[x].pb(id[idd[pp]]);
return;
}
if(L<=m) get(lson[x],l,m,L,R);
if(m<R) get(rson[x],m+1,r,L,R);
}
bool fl[MAXN]={};
int iid[MAXN];
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
For(i,n) {
a[i]=read();
}
build(1,1,n);
int m=read();
int rot=1;
int newid=n;
For(i,n) idd[i]=i;
For(i,m) {
int op=read();
if(op==1) {
int x=read(),l=read(),r=read();
pp=x;
get(rot,1,n,l,r );
}else {
int x=read(),p=read();
int ro=0;
init(ro,rot,1,n,x,++newid);
rot=ro;
a[newid]=p;

idd[x]=newid;
}
}
tot++;
find_scc(tot);

For(i,tot) {
for(int v: G[i]){
if(sccno[i]!=sccno[v]) {
fl[sccno[i]]=1;
}
}
}
For(i,newid){
iid[i]=sccno[id[i]];
}

For(i,newid){
sccans[sccno[id[i]]]+=a[i];
}
ll p=1e18;
For(i,newid) if(!fl[iid[i]]){
gmin(p,sccans[iid[i]]);
}

cout<<p<<endl;
return 0;
}


举报

相关推荐

0 条评论