Codeforces Round #639 (Div. 2) May/06/2020 22:35UTC+8

比赛链接 https://codeforces.com/contest/1344
比赛记录 https://blog.csdn.net/cheng__yu_/article/details/105395197

A. Puzzle Pieces

在这里插入图片描述
思路:成立条件只有 n = 1 或者 m = 1 或者 n == 2 && m ==2

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=3e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;

int t;
int n,m;

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		if(n>m)
			swap(n,m);
		if(n==1)
			puts("YES");
		else
		{
			if(n==2&&m==2)
				puts("YES");
			else
				puts("NO");
		}
	}
    return 0;
}

B. Card Constructions

在这里插入图片描述
题意:给定 k 根木棍,每次都拼最大的金字塔,问能够拼成几个金字塔
思路

  • 设 n 为 层高,可以得到公式: ( 1 + n ) × n 2 × 3 − n \frac {(1+n)\times n}2 \times 3 - n 2(1+n)×n×3n
  • 对公式预处理之后,二分一下
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t;
ll n,a[maxn];

int main()
{
	for(ll i=1;i<=40000;i++)
		a[i]=(1+i)*i/2*3-i;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int cnt=0;
		while(n)
		{
			int p=upper_bound(a+1,a+1+40000,n)-a-1;
			if(p==0)
				break;		
			n-=a[p];
			cnt++;
		}
		cout<<cnt<<"\n";
	}
	return 0;
}

C. Hilbert’s Hotel

在这里插入图片描述
题意:数轴上的每一个整数表示有一位客人,对于每一个数 k,变换之后的位置为: k + a k % n k+a_{k \% n} k+ak%n。问是否有两位客人会出现在同一个点上

思路:可以发现:模n同余的数,都会走相同的距离。比如,n=4,0、4、8,就会走相同的路。
移动后的位置可以这样表示

  • 余数为0的数: k 0 n + a 0 k_0n+a_0 k0n+a0
  • 余数为1的数: k 1 n + a 1 + 1 k_1n+a_1+1 k1n+a1+1
  • 余数为2的数: k 2 n + a 2 + 2 k_2n+a_2+2 k2n+a2+2
  • 余数为3的数: k 3 n + a 3 + 3 k_3n+a_3+3 k3n+a3+3

只要满足每一类数不占据相同的位置,就不会有重复。也就是判断 ( ( a i + i ) % n + n ) % n ((a_i+i)\%n+n)\%n ((ai+i)%n+n)%n,有没有重复出现

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t;
ll n,a[maxn];

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld",&n);
		for(int i=0;i<n;++i)
			scanf("%lld",&a[i]);
		map<ll,bool> m;
		bool valid=true;
		for(int i=0;i<n;++i)
		{
			ll pos=((a[i]+i)%n+n)%n;
			if(m.count(pos))
			{
				valid=false;
				break;
			}
			else
				m[pos]=true;
		}	
		if(valid)
			puts("YES");
		else
			puts("NO");	
	}
	return 0;
}

D. Monopole Magnets(思维 + DFS)*

在这里插入图片描述
题意:地图有黑白两种颜色组成。你可以在地图上放置北极和南极的磁铁,北极磁铁可以向南极移动。要求

  • 每行每列都必须有一个南极
  • 北极通过南极的牵引,可以到底任意一个黑色的格子
  • 北极通过南极的牵引,不可以到底任意一个白色的格子

求最少放置的北极数量

思路

  • 空行和空列必须同时出现,这样才能在空行上放置南极
  • 在同一行中黑色的连续段不能出现两次
  • 排除了以上两种情况之后,只需要求连通块就好了
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e3+10,maxm=1e5+5;
const int inf=0x7f7f7f7f;

int n,m;
char g[maxn][maxn],visit[maxn][maxn];
int row[maxn],col[maxn];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

bool check()
{
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            if(g[i][j]=='#')
            {
                if(row[i]&&j-row[i]>1)
                    return 0;
                if(col[j]&&i-col[j]>1)
                    return 0;
                row[i]=j;
                col[j]=i;
            }
        }
    }

    bool f1=0,f2=0;
    for(int i=1;i<=n;++i)
        if(row[i]==0)
        {
            f1=1;
            break;
        }
    for(int i=1;i<=m;++i)
        if(col[i]==0)
        {
            f2=1;
            break;
        }
    if(f1^f2)
        return 0;
    return 1;
}

void dfs(int x,int y)
{
    for(int i=0;i<=3;++i)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&g[nx][ny]=='#'&&!visit[nx][ny])
        {
            visit[nx][ny]=1;
            dfs(nx,ny);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        scanf("%s",g[i]+1);
    if(!check())
    {
         puts("-1");
         return 0;
    }
    int cnt=0;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(!visit[i][j]&&g[i][j]=='#')
            {
                cnt++;
                visit[i][j]=1;
                dfs(i,j);
            }
    printf("%d\n",cnt);
	return 0;
}

E. Quantifier Question(拓扑排序)

在这里插入图片描述

题目中已经说明了,先后顺序很重要。而且只能是按: Q 1 x 1 , Q 2 x 2 … , Q n x n Q_1x_1,Q_2x_2\dots,Q_nx_n Q1x1,Q2x2,Qnxn的顺序排列

  • 可以发现: ∀ x , ∃ y , x < y \forall x,\exists y,x<y x,y,x<y是成立的:任意一个 x x x都存在一个 y y y,使得 x < y x<y x<y
  • 同样的: ∀ x , ∃ y , x > y \forall x,\exists y,x>y x,y,x>y也是成立的:任意一个 x x x都存在一个 y y y,使得 x > y x>y x>y

所以对于 x 3 < x 1 , x 3 < x 2 x_3<x_1,x_3<x_2 x3<x1x3<x2的描述, ∀ x 1 ∀ x 2 ∃ x 3 \forall x_1\forall x_2 \exists x3 x1x2x3 x 1 > x 3 , x 2 > x 3 x_1>x_3,x_2>x_3 x1>x3,x2>x3是成立的

基本想法:建立正图和反图dfs跑拓扑排序,如果发现存在环,则答案为 -1。否则的话,入度为0的设为A,其余的设为E

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;

int n,m;
vector<int> G1[maxn],G2[maxn];
int c1[maxn],c2[maxn];

bool dfs1(int u)
{
    c1[u]=-1;
    for(auto v : G1[u])
    {
        if(c1[v]==-1)
            return false;
        if(!c1[v]&&!dfs1(v))
            return false;
    }
    c1[u]=1;
    return true;
}

bool dfs2(int u)
{
    c2[u]=-1;
    for(auto v : G2[u])
    {
        if(c2[v]==-1)
            return false;
        if(!c2[v]&&!dfs2(v))
            return false;
    }
    c2[u]=1;
    return true;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        G1[u].push_back(v);
        G2[v].push_back(u);
    }
    int cnt=0;
    string ans="";
    for(int i=1;i<=n;++i)
    {
        if(!c1[i]&&!c2[i])
        {
            cnt++;
            ans+='A';
        }
        else
            ans+='E';
        if(!dfs1(i)||!dfs2(i))
        {
            puts("-1");
            return 0;
        }
    }
    cout<<cnt<<"\n";
    cout<<ans<<"\n";
    return 0;
}

F. Résumé Review(二分增量)

在这里插入图片描述
思路 Δ = f ( x ) − f ( x − 1 ) = a − 3 x 2 + 3 x − 1 \Delta =f(x)-f(x-1)= a-3x^2+3x-1 Δ=f(x)f(x1)=a3x2+3x1,随着每个 b_i 选择量的增加,增量递减。我们需要选择前 k 个最大的增量。

  • 首先二分这个增量,一开始增量是最大的,增量越大选择的总数量越小
  • 然后根据这个增量,二分每一个 b i b_i bi b i b_i bi 越大增量越小,从而确定每一个 b i b_i bi 的值
  • 最后得到的总数量,会大于 k k k ,所以需要把选择到的相对较小的增量删去
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;

ll n,k,b[maxn];
ll a[maxn];

ll calc(ll ai,ll x)
{
    return ai-3*x*x+3*x-1;
}

ll check(ll d)
{
    ll cnt=0;
    for(int i=1;i<=n;++i)
    {
        ll L=1,R=a[i];
        while(L<R)
        {
            int mid=(L+R+1)>>1;
            if(calc(a[i],mid)>=d)
                L=mid;
            else
                R=mid-1;
        }
        cnt+=L;
        b[i]=L;
    }
    return cnt;
}

int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;++i)
        scanf("%lld",&a[i]);

    ll L=-4e18,R=4e18;
    while(L<R)
    {
        ll mid=(L+R+1)>>1;
        if(check(mid)>=k)
            L=mid;
        else
            R=mid-1;
    }
    ll s=check(L);
    for(int i=1;i<=n;++i)
    {
        if(s>k&&calc(a[i],b[i])==L)
            s--,b[i]--;
    }
    for(int i=1;i<=n;++i)
        printf("%lld%c",b[i],i==n?'\n':' ');
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值