2021.9.114考试总结[NOIP模拟53]

2021-09-15 08:02:18  阅读：18  来源： 互联网

T1 ZYB和售货机

\$code:\$

``` 1 #include<bits/stdc++.h>
2 #define int long long
3 using namespace std;
4
5 namespace IO{
7         char ch=getchar(); int x=0,f=1;
8         while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
9         while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
10         return x*f;
11     }
12     inline void write(int x,char sp){
13         char ch[20]; int len=0;
14         if(x<0){ putchar('-'); x=~x+1; }
15         do{ ch[len++]=x%10+(1<<4)+(1<<5); x/=10; }while(x);
16         for(int i=len-1;~i;--i) putchar(ch[i]); putchar(sp);
17     }
18     inline int max(int x,int y){ return x<y?y:x; }
19     inline int min(int x,int y){ return x<y?x:y; }
20     inline void swap(int &x,int &y){ x^=y^=x^=y; }
21     inline void chmax(int &x,int y){ x=x<y?y:x; }
22     inline void chmin(int &x,int y){ x=x<y?x:y; }
23 } using namespace IO;
24
25 const int NN=1e6+5;
26 int n,st,ans,cnt,mn,bel[NN],f[NN],c[NN],d[NN],a[NN],mx[NN],sc[NN],val[NN];
27 bool vis[NN];
28
29 void dfs(int x){
30     if(bel[x]==cnt) return ans-=mn,void();
31     if(bel[x]) return;
32     if(!mx[x]) return;
33     bel[x]=cnt;
34     ans+=val[mx[x]]*a[x];
35     chmin(mn,val[mx[x]]-val[sc[x]]);
36     if(mx[x]!=x)dfs(mx[x]);
37 }
38 signed main(){
39     freopen("goods.in","r",stdin);
40     freopen("goods.out","w",stdout);
42     for(int i=1;i<=n;i++)
44     for(int i=1;i<=n;i++){
45         val[i]=d[f[i]]-c[i];
46         if(val[i]>val[mx[f[i]]]) sc[f[i]]=mx[f[i]], mx[f[i]]=i;
47         else if(val[i]>val[sc[f[i]]]) sc[f[i]]=i;
48     }
49     for(int i=1;i<=n;i++)
50         if(!bel[i]) ++cnt,mn=INT_MAX,dfs(i);
51     write(ans,'\n');
52     return 0;
53 }```
T1