绵阳东辰国际test2019.10.31eve

吐槽:

我真的是服了出题人了,给结论又不说证明,它是什么鬼东西

为什么他们都能猜出结论?还是我想法太过复杂,想的太多?

分析:

结论是:

ans=max(ai,Σai/m),Σai/m向上取整

code:

#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
 
inline ll read() {
    ll x = 0, f = 1; char ch = getchar();
    for(; ch < '0' || ch>'9'; ch = getchar())
        if(ch == '-') f = -f;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        x = x * 10 + ch - '0';
    return x * f;
}
 
inline void chkmin( int &a, int b ) { if(a > b) a = b; }
 
inline void chkmax( int &a, int b ) { if(a < b) a = b; }
 
#define _ read()
 
#define ln endl
 
const int N = 4e5 + 5;
 
multiset<ll> s;
int n, m, q, a[N];
ll sum;
 
int main()
{
    n = _; m = _; q = _;
    for( int i = 1; i <= n; i++ )
        a[i] = _, sum = sum + a[i], s.insert(a[i]);
    ll  need = (sum - 1ll) / m + 1ll;
    cout << max(need, (*s.rbegin())) << ln;
    while(q--)
    {
        int x = _, p = _;
        s.erase(s.find(a[x]));
        sum = sum - a[x] + p;
        a[x] = p;
        s.insert(a[x]);
        need = (sum - 1ll) / m + 1ll;
        cout << max(need, (*s.rbegin())) << ln;
    }
}

吐槽:

思路完全没有问题,但我用Tarjan写的,只用dfs就解决的

我发誓,以后不到迫不得已的时候绝对不会再写Tarjan

分析:

简单说一下思路

先将不是环上的点能删掉就删掉,

再按照环的大小从大到小的顺序去删

这样保证最优

附上我wa了4个点的code:

#include<bits/stdc++.h>
#define ll long long
#define il inline
#define ri register int
#define lowbit(x) x&(-x)
using namespace std;
const int maxn=1e6+5;
const int maxm=1e6+5;
int top,k,n,m,cnt,sum,tot,momo,cnt1;
int dfn[maxn],stk[maxn],low[maxn],sz[maxn],bel[maxn];
bool vis[maxn];
vector<int>Q[maxn];
il void Tarjan(int u,int fa){
	dfn[u]=low[u]=++cnt;
	vis[u]=true;stk[++top]=u;
	for(ri i=0;i<Q[u].size();i++){
		int v=Q[u][i];if(v==fa)continue;
		if(!dfn[v])Tarjan(v,u),low[u]=min(low[u],low[v]);
		else if(vis[v])low[u]=min(low[u],dfn[v]);
	} 
	if(low[u]==dfn[u]){
		sum++;
		while(stk[top]!=u){
			int tmp=stk[top];
			vis[tmp]=false;
			bel[tmp]=sum;
			sz[sum]++;top--;
		}
		vis[u]=false;bel[u]=sum;sz[sum]++;top--;
	}
}
priority_queue<int,vector<int>,less<int> >T;
int main(){
	freopen("3.in","r",stdin);
	//freopen("2.out","w",stdout);
	scanf("%d%d%d",&n,&m,&k);
	for(ri i=1,u,v;i<=m;i++){
		scanf("%d%d",&u,&v);
		Q[u].push_back(v);
		Q[v].push_back(u);
	}
	
	for(ri i=1;i<=n;i++)
		if(!dfn[i]){
			Tarjan(i,i);momo++;
			if(sz[bel[i]]==1&&!Q[i].size())cnt1++;
		}
	if(k==0){printf("%d\n",momo);return 0;}
	if(k>=m){printf("%d\n",n);return 0;}
	for(ri i=1;i<=sum;i++)if(sz[i]!=1)T.push(sz[i]);
	tot=sum;
	tot-=cnt1;tot--;
	if(k<=tot){printf("%d\n",momo+k);return 0;}
	else {
		k-=tot;int ans=tot+momo;
		while(!T.empty()){
			int su=T.top();T.pop();
			k--;
			if((su-1)>=k){
				ans+=k;
				printf("%d\n",ans);return 0;
			}
			else k-=(su-1),ans+=su;
		}
		printf("%d\n",ans);return 0;
	}
}

AC code:

#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=4e6+10;
int pre[maxn],last[maxn],other[maxn],l;
void add(int x,int y)
{
    l++;
    pre[l]=last[x];
    last[x]=l;
    other[l]=y;
}
int n,m,k;
int dfn[maxn],low[maxn],cnt;
int belong[maxn],qw;
int inbe[maxn];
stack<int> s;
int cntbloc;
int cir[maxn];
int dis[maxn];
bool cmp(int x,int y){return x>y;}
int ans;
int num;
void dfs(int x,int fa)
{
    for(int p=last[x];p;p=pre[p])
    {
        int v=other[p];
        if(v==fa) continue;
        if(!dis[v])
        {
            dis[v]=dis[x]+1;
            dfs(v,x);
        }
        else if(dis[v]<dis[x])
        {
            cir[++num]=dis[x]-dis[v]+1;
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }  
     
    for(int i=1;i<=n;i++)
    {
        if(!dis[i])
        {
            dis[i]=1;
            dfs(i,i);
            cntbloc++;
        }
    }
    int sumbridge=0;
    for(int i=1;i<=n;i++) sumbridge+=cir[i];
        sumbridge=m-sumbridge;
    if(k<=sumbridge)
    {
        printf("%d\n",k+cntbloc);
        return 0;
    }
    else
    {
        sort(cir+1,cir+num+1,cmp);
        ans=cntbloc+sumbridge;
        k-=sumbridge;
        for(int i=1;i<=num;i++)
        {
            if(cir[i]==1) continue;
            if(cir[i]<=k)
            {
                ans+=cir[i]-1;
                k-=cir[i];
            }
            else
            {
                ans+=k-1;
                break;
            }
        }
        printf("%d",ans);
    }
    return 0;
}

T3暴力也打卦了?
看来我的代码功底不是一般的差

posted @ 2019-11-01 10:33  wzx_believer  阅读(153)  评论(0编辑  收藏  举报