UPC 2021个人训练赛第11场 题解_761128315856702-程序员宅基地

技术标签: dfs  算法  c++  树结构  # 算法多校训练  动态规划求解  # ACM特殊算法  

什么,你问我维森莫有的题目不能AC,那我告诉你,下面这个骂的就是我!在这里插入图片描述

问题 A: God Sequence

在这里插入图片描述

样例输入
【样例1】 1 1
【样例2】 1 4
【样例3】 7 5
样例输出
【样例1】 1001 -1001
【样例2】 -8 -6 -9 120 -97
【样例3】 323 -320 411 206 -259 298 -177 -564 167 392 -628 151

提示
样例1解释
A sequence (1001,−1001) contains A=1 positive integer and B=1 negative integer totaling 1001+(−1001)=0. It also satisfies the other conditions and thus is a god sequence.
样例2解释
A sequence (−8,−6,−9,120,−97) contains A=1 positive integer and B=4 negative integers totaling (−8)+(−6)+(−9)+120+(−97)=0. It also satisfies the other conditions and thus is a god sequence.

签到题,模拟

#include <bits/stdc++.h>
#define ll long long  
using namespace std;
int n,m,ans=0;

int main()
{
    
	ios::sync_with_stdio(false);
	cin.tie(0);  cout.tie(0);
	cin >> n >> m;
	if(n==m) 
		for(int i=1; i<=n; i++)	
		   cout << i << " " << i*(-1) << " ";
	else if(n>m) 
	{
    
		for(int i=1; i<=m-1; i++)
			cout << i << " " << i*(-1) << " ";
		for(int i=m; i<=n; i++)	{
    
			cout << i << " ";
			ans += i;
		}
		cout << ans*(-1);
	}
	else 
	{
    
		for(int i=1; i<=n-1; i++)	
		    cout << i << " " << i*(-1) << " ";
		for(int i=n; i<=m; i++)	{
    
			cout << i*(-1) << " ";
			ans += i;
		}
		cout << ans << endl;
	}
	return 0;
}

问题 B: ARC Wrecker

在这里插入图片描述

样例输入
【样例1】 2 1 2
【样例2】 6 5 3 4 1 5 2
【样例3】 7 314 159 265 358 979 323 846
样例输出
【样例1】 4
【样例2】 32
【样例3】 492018656

提示
样例1解释
There are four possible combinations of heights of the buildings, as follows:
(Building 1, Building 2) = (0,0) (Building 1, Building 2) = (0,1)
(Building 1, Building 2) = (1,1) (Building 1, Building 2) = (1,2)
样例3解释
There are 20192492160000 possible final sceneries. The correct output is that number modulo 109+7, which is 492018656.

除了不同的数会变得相同,其他相对大小不会改变。
设高度取值(包括最低点 0)排序后的集合为 a,独立选择把每个间距(ai+1−ai)减到多少,
答案就是 ∏(ai+1−ai+1)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M=1e9+7,N=1e5;
int n,a[N],res;

struct op {
    
	int x; 
	op(int x):x(x) {
    }
	friend op operator + (const op &a, const op b) {
    
	    const int x = a.x+b.x; 
	    return op(x>=M?x-M:x);
	}
	friend op operator - (const op &a, const op b) {
    
	    const int x = a.x-b.x; 
	    return op(x<0?x+M:x);
	}
	friend op operator * (const op &a, const op b) {
    
	    return op(1ll*a.x*b.x%M);
	}
};

int ok(int a, int x=M-2, int res=1) {
    
	for(; x; x>>=1, a=(op(a)*a).x)
	    if(x&1)  res=(op(res)*a).x;
	return res;
}

int main() 
{
    
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>n;
	for(int i=0; i<n; ++i)  
	    cin>>a[i];
	sort(a,a+n), n = unique(a,a+n)-a;
	res = a[0]+1;
	for(int i=0; i<n-1; ++i)
	    res = (op(res)*(a[i+1]-a[i]+1)).x;
	cout<<res<<endl;
	return 0;
}

问题 C: Tricolor Pyramid

在这里插入图片描述

样例输入
【样例1】 3 BWR
【样例2】 4 RRBB
【样例3】 6 BWWRBW
【样例4】 8 WWBRBBWB
【样例5】 21 BWBRRBBRWBRBBBRRBWWWR
样例输出
【样例1】 W
【样例2】 W
【样例3】 B
【样例4】 R
【样例5】 B

提示
样例1解释
In this case, we will pile up blocks as follows: the 1-st and 2-nd blocks from the left in the bottom row are respectively blue and white, so we place a red block on top of it;
the 2-nd and 3-rd blocks from the left in the bottom row are respectively white and red, so we
place a blue block on top of it;
the blocks in the 2-nd row from the bottom are respectively red and blue, so we place a white block on top of it. Thus, the block at the top will be white;
we should print W.

样例2解释
In this case, we will pile up blocks as follows:
the 1-st and 2-nd blocks from the left in the bottom row are both red, so we place a red block on top of it;
the 2-nd and 3-rd blocks from the left in the bottom row are respectively red and blue, so we place
a white block on top of it;
the 3-rd and 4-th blocks from the left in the bottom row are both blue, so we place a blue block on top of it;
the 1-st and 2-nd blocks from the left in the 2-nd row from the bottom are respectively red and white, so we place a blue block on top of it;
the 2-nd and 3-rd blocks from the left in the 2-nd row from the bottom are respectively white and blue, so we place a red block on top of it;
the blocks in the 3-rd row from the bottom are respectively blue and red, so we place a white block on top of it. Thus, the block at the will be white;
we should print W.
在这里插入图片描述

公式转换:a⊗b=−(a+b)mod3
组合数算贡献即可。处理阶乘时如果 n≥3,就会 n!≡0(mod3)
所以维护一个数的同时维护它当中因数 3 的个数
在这里插入图片描述

#include <bits/stdc++.h>
#define ll long long  
using namespace std;
const int N=4e5,M=3;
string s;
int n,res; 

struct op {
    
	int x; 
	op(int x):x(x) {
    }
	friend op operator + (const op &a, const op b) {
    
	    const int x = a.x+b.x; 
	    return op(x>=M?x-M:x);
	}
	friend op operator - (const op &a, const op b) {
    
	    const int x = a.x-b.x; 
	    return op(x<0?x+M:x);
	}
	friend op operator * (const op &a, const op b) {
    
	    return op(1ll*a.x*b.x%M);
	}
};

int ok(int a, int x=M-2, int res=1) {
    
	for(; x; x>>=1, a=(op(a)*a).x)
	    if(x & 1)  res=(op(res)*a).x;
	return res;
}

struct par {
    
	int p,c;
	par(int x):p(x),c(0) {
     while (!(p%M)) {
    p/=M,++c;} }
};

int main() 
{
    
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>n>>s;
	par f=1;
	for(int i=0; i<n; i++) {
    
		int x = s[i]=='B'?0:(s[i]=='W'?1:2);
		if(!f.c)  res = (res+op(f.p)*x).x;
		if(i==n-1)  break;
		par m = n-1-i, d=i+1;
		f.p = (op(f.p)*m.p*ok(d.p)).x;
		f.c = f.c+m.c-d.c;
	}
	if(!(n&1))  res = (op(0)-res).x;
	cout<<(res==0?'B':(res ==1?'W':'R'))<<endl;
	return 0;
}

问题 D: Miracle Tree

在这里插入图片描述
在这里插入图片描述

无根树必须找根。必然有一个点 u 满足 Eu=1,就设它为根。

对于两条叶子到根的链,它们受到根的距离的约束必然没有互相之间距离的约束大,但是又不足以大到要把 Eu 跟深度反着标。所以标法必然是维护一个时间,DFS 整棵树,在一个节点进栈的时候时间 +1,出栈的时候时间 +1,然后一个节点的 Eu 为进栈时间。

会发现最大的标号的节点(终止节点) v 的标号是 2n−dep[v]。所以根和终止节点取直径的两端即可。

#include <bits/stdc++.h>
#define ll long long  
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
using namespace std;
const int N = 200000+10;
int n,d[N],rt,h[N],a[N],ia=1;
vector<int> G[N];

void bfs(int s) {
    
	for(int u=0; u<n; u++)  d[u]=-1;
	queue<int> q; 
	q.push(s), d[s]=0;
	while(sz(q)) {
    
		int u = q.front(); 
		q.pop();
		for(const int &v : G[u]) {
    
		   if(!~d[v])  
		       d[v] = d[u]+1, q.push(v);
		}
	}
}

void GetH(int u, int fa=-1) {
    
	h[u]=-1;
	for(const int &v : G[u]) {
    
	    if(v!=fa)
	        GetH(v,u), h[u]=max(h[u],h[v]);
	}
	++h[u];
}

void GetA(int u, int fa=-1) 
{
    
	a[u] = ia++;
	for(const int &v:G[u])  
	    if(v!=fa)  GetA(v,u);
	++ia;
}

int main() 
{
    
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>n;
	for(int i=0; i<n-1; i++) {
    
		int u,v; 
		cin>>u>>v,--u,--v;
		G[u].push_back(v), G[v].push_back(u);
	}
	bfs(0), rt = max_element(d,d+n)-d;
	GetH(rt);
	for(int u=0; u<n; u++) {
    
	    sort(all(G[u]), [&](const int &i, const int &j) {
    return h[i] < h[j];});}
	GetA(rt);
	for(int u=0; u<n; u++) 
	    cout<<a[u]<<' '; 
	cout << endl;
	return 0;
}

问题 E: Zero-Sum Ranges 2

在这里插入图片描述

样例输入
【样例1】 1 1
【样例2】 2 3
【样例3】 3 7
【样例4】 8 24
【样例5】 30 230
【样例6】 25 455
样例输出
【样例1】 2
【样例2】 2
【样例3】 6
【样例4】 568
【样例5】 761128315856702
【样例6】 0
在这里插入图片描述

注意数组大小!容我偷个题解…
在这里插入图片描述

#include <bits/stdc++.h>
#define ll long long  
using namespace std;
ll ans,c[62][62],f[62][62*62][62];
int n,m;

int main()
{
    
    scanf("%d%d",&n,&m);
    for(int i=0; i<65; i++) {
    
        c[i][0] = c[i][i]=1;
        for(int j=1; j<i; j++)
		    c[i][j] = c[i-1][j-1]+c[i-1][j];
    }
    
	for(int i=1; i<=n+1; i++)
	    f[i][c[i][2]][i-1] = 1;
   
    for(int i=1; i<=2*n+1; i++)
        for(int j=0; j<=m; j++)
            for(int k=0; k<i; k++)
                for(int x=k+2; x<=2*n+1-i; x++)
                    if (j+c[x][2]<=m)
					    f[i+x][j+c[x][2]][x-(k+2)] += c[x-1][k+1]*f[i][j][k];
   
    for(int i=1; i<=2*n+1; i++)
        for(int j=0; j<=m; j++)
            for(int k=1; k<i; k++)
                for(int x=k; x<=2*n+1-i; x++)
                    if (j+c[x][2]<=m)
					    f[i+x][j+c[x][2]][x-k] += c[x-1][k-1]*f[i][j][k];
    
	printf("%lld",ans+f[2*n+1][m][0]);
    return 0;
}

问题 G: 展示玩具

在这里插入图片描述
在这里插入图片描述

sort一下,二分

#include <bits/stdc++.h>
#define ll long long  
using namespace std;
const int N=1e4+10;
int n,k,maxn,a[N];

int main()
{
    
	cin>>n>>k;
	for(int i=1; i<=n; i++)	cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1; i<=n; i++)
	{
    
		int l=i,r=n;
		while(l<r)
		{
    
			int mid=(l+r+1)/2;
			if(a[mid]<=a[i]+k)  l=mid;
			else r=mid-1;
		}
		maxn=max(maxn,l-i+1);
	}
	cout<<maxn<<endl;
	return 0;
}

问题 K: 促销骰子

在这里插入图片描述
在这里插入图片描述

这提示给的,难度直接降到最低…直接分类讨论

#include <bits/stdc++.h>
#define ll long long  
using namespace std;
int a,b,c,t;

int main()
{
    
	cin>>a;
    if(a!=6)  cout<<"0"<<endl;
    else if(a==6)  
    {
    
    	cin>>b;
    	if(b!=6) cout<<"10"<<endl;
    	else {
    
    		cin>>c;
    		if(c!=6) cout<<"100"<<endl;
    		else cout<<"1000"<<endl;
		}
	}
	return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Luoxiaobaia/article/details/117135416

智能推荐

Ubuntu16.04 win10 双系统 电脑开机进入 grub rescue 状态_set boot=-程序员宅基地

文章浏览阅读2.3k次。Ubuntu 16.04 win10 双系统 电脑开机进入 grub rescue 状态先初始大图:这应该是小问题,百度好多 ls # 找出Ubuntu所在的磁盘,我这里是 (hd0,gpt7) set boot=(hd0,gpt7) set prefix=(hd0,gpt7)/grub insmod normal normal不出意外可以正常启动,但是,但是,一般都..._set boot=

Hadoop格式化报错java.net.UnknownHostException:_shutdown_msg: shutting down namenode at kilon-virt-程序员宅基地

文章浏览阅读9.1k次,点赞2次,收藏6次。异常描述在对HDFS格式化,执行Hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示:[plain] view plain copy [shirdrn@localhost bin]$ hadoop namenode -format 11/06/22 07:33:31 INFO_shutdown_msg: shutting down namenode at kilon-virtual-machine/127.0.1.1

基于注意力机制attention结合门控循环单元GRU多维时间序列预测,GRU-Attention时间序列预测,多输入单输出模型。运行环境MATLAB版本为2020b及其以上。评价指标包括:R_gru和attention结合-程序员宅基地

文章浏览阅读159次。基于注意力机制attention结合门控循环单元GRU多维时间序列预测,GRU-Attention时间序列预测,多输入单输出模型。运行环境MATLAB版本为2020b及其以上。评价指标包括:R2、MAE、MSE、RMSE等,代码质量极高,方便学习和替换数据。_gru和attention结合

idea启动sentinel说找不到或无法加载该类 .port=8180_找不到或无法加载主类 .port=8180-程序员宅基地

文章浏览阅读1.4k次,点赞9次,收藏4次。idea启动sentinel说找不到或无法加载该类 .port=8180一、正常启动流程-Dserver.port=8180 -Dcsp.sentinel.dashboard.server=localhost:8180 -Dproject.name=sentinel-dashboard -jar (添加sentinel目录路径)sentinel-dashboard-1.8.0.jar二、出现说找不到或无法加载该类 .port=8180的解决方案idea问题新版不能直接识别命令参数要加单引号 ‘_找不到或无法加载主类 .port=8180

Redis序列化存储及其日期格式问题_redis 时间格式-程序员宅基地

文章浏览阅读4.7k次,点赞3次,收藏8次。万花从中过片页不沾身在模块开发中,使用Redis做缓存是非常常见的技术,当我们注入RedisTempate模板时redisTemplate.opsForValue().set("item_"+id,itemModel,10, TimeUnit.MINUTES);key我们可以用固定开头和商品id进行拼接,当然正常的项目开发中最好使用多级目录进行分类,这里只做演示使用可视化界面看到保存的数据是这样的这样的数据是很不容易阅读的,原因是Redis默认使用的是JAVA序列化方式,在序列化时使用了._redis 时间格式

编译原理学习(三)--语法分析树-程序员宅基地

文章浏览阅读3.4w次,点赞7次,收藏28次。语法分析树用图形方式展现了从文法的开始符号推导出相应语言中的符号串的过程。在具体理解语法分析树之前需要先理清楚一些基本概念:①.产生式用变量expr来表示表达式,用变量stmt表示语句,那么这个构造规则可以表示为:stmt---&gt;if(expr)stmtelse stmt其中的箭头(---&gt;)可以读作“可以具有以下形式”,这样的规则称为产生式。②.文法定义关于文法定义中的终结符和非终结..._语法分析树

随便推点

ubuntu下OpenPose的安装、使用、初步介绍_ubuntu 安装openpose-程序员宅基地

文章浏览阅读2.1w次,点赞11次,收藏119次。安装之前你需要安装caffe。OpenPose 是第一个实时多人姿态估计系统,包括人体姿态、手指、面部表情等,总共有可以检测135个关键点。特点:功能:2D 实时多人关键点检测:15、18或25个身体/脚步的关键点检测,运算时间与检测出的人物数无关。2*21个手部关键点检测,运算时间取决于检测出的人数。70个人脸关键点检测,运算时间取决于检测出的人数。3D 实时单人关..._ubuntu 安装openpose

关于Vue中this作用域说明,以及一个this->undefined问题的处理_为什么this undefined-程序员宅基地

文章浏览阅读5.1k次,点赞4次,收藏7次。尝试在Vue中的methods定义几个函数,实现函数A调用函数B的需求。但是发现通过this关键字引用会提示 B函数not defined报错信息如下:实际上个问题的根本原因是函数以及子函数的this作用域范围处理问题Vue中this作用域说明参考文章:Vue里this指向Vue官网对Methods的解析如何修复Vue中的 “this is undefined” 问题对于一个..._为什么this undefined

加载MNIST报错:[WinError 10060] 由于连接方在一段时间后没有正确答复解决办法_urlerror: <urlopen error [winerror 10060] 由于连接方在一段-程序员宅基地

文章浏览阅读2.5w次,点赞18次,收藏20次。转载:https://blog.csdn.net/landcruiser007/article/details/79346982tensorflow加载mnist数据集,一些书上和博客中的代码如下:from tensorflow.examples.tutorials.mnist import input_datamnist=input_data.read_data_sets("MNIS..._urlerror:

【Linux基础】第16讲 mkdir和rm命令选项-程序员宅基地

文章浏览阅读181次。不使用-p命令来创建文件夹依赖。使用-p来创建文件夹依赖。

jenkins重启 linux_Jenkins 自动部署-程序员宅基地

文章浏览阅读346次。连接服务器 首先要有一台自己的服务器,或者本机装一个虚拟机,系统最好使用Linux,用了Linux才能感觉到是真的比win好用。我就用自己的云服务器来举例。 打开终端使用ssh连接到服务器 ssh [email protected] 输入密码 ******安装组件 首先检查系统中是否安装java,可以用java -version,如果显示出java版本证明java已经安装,否..._liunx jenkins二进制怎么重启

关于用transmac黑苹果制作引导盘无法识别_transmac u盘 无法引导 csdn-程序员宅基地

文章浏览阅读3.5k次。第一种方法https://www.apple114.com/pages/macos/可以从这儿冲39块钱开通会员下载,好心人可以帮我下载一下,嘤嘤嘤第二种方法利用VMware安装虚拟器macos系统制作启动盘,比较麻烦参考教程https://www.cnblogs.com/macz/p/14863851.html插入u盘,将U盘在「磁盘工具」中初始化,并在「终端」输入一下命令,其中「MyVolume」代表自己的U盘名称,重要的事再说一遍 MyVolume 是你U盘的名字,格式化之后U盘的名字_transmac u盘 无法引导 csdn

推荐文章

热门文章

相关标签