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

智能推荐

射频识别技术漫谈(6-10)_芯片 ttf模式-程序员宅基地

文章浏览阅读2k次。射频识别技术漫谈(6-10),概述RFID的通讯协议;射频ID卡的原理与实现,数据的传输与解码;介绍动物标签属性与数据传输;RFID识别号的变化等_芯片 ttf模式

Python 项目实战 —— 手把手教你使用 Django 框架实现支付宝付款_django 对接支付宝接口流程-程序员宅基地

文章浏览阅读1.1k次。今天小编心血来潮,为大家带来一个很有趣的项目,那就是使用 Python web 框架 Django 来实现支付宝支付,废话不多说,一起来看看如何实现吧。_django 对接支付宝接口流程

Zabbix 5.0 LTS在清理历史数据后最新数据不更新_zabbix问题没有更新-程序员宅基地

文章浏览阅读842次。Zabbix 5.0 LTS,跑了一年多了一直很稳定,前两天空间显示快满了,于是手贱清理了一下history_uint表(使用mysql truncate),结果折腾了一周。大概故障如下:然后zabbix论坛、各种群问了好久都没解决,最后自己一番折腾似乎搞定了。初步怀疑,应该是由于历史数据被清空后,zabbix需要去处理数据,但是数据量太大,跑不过来,所以来不及更新了(?)..._zabbix问题没有更新

python学习历程_基础知识(2day)-程序员宅基地

文章浏览阅读296次。一、数据结构之字典 key-value

mybatis-plus字段策略注解strategy_mybatisplus strategy-程序员宅基地

文章浏览阅读9.7k次,点赞3次,收藏13次。最近项目中遇到一个问题,是关于mybatis-plus的字段注解策略,记录一下。1问题调用了A组件(基础组件),来更新自身组件的数据,发现自己组件有个字段总是被清空。2原因分析调用的A组件的字段,属于基础字段,自己业务组件,对这个基础字段做了扩展,增加了业务字段。但是在自己的组件中的实体注解上,有一个注解使用错误。mybatis-plus封装的updateById方法,如果..._mybatisplus strategy

mx250显卡天梯图_2020年最新笔记本显卡天梯图,看看你的显卡排在哪!-程序员宅基地

文章浏览阅读1.5k次。显卡天梯图就是显卡的性能排行榜,目前显卡主要有Nvidia(英伟达)和AMD(超微半导体)两大品牌。我们都知道,显卡性能决定了电脑的图像处理能力。对于喜欢玩游戏的电脑用户来说,处理器和显卡是用户最关心的电脑硬件,一块好的显卡对于游戏的运行效率、画面显示会起到重要作用。下面小编就和大家分享一下2020年1月笔记本显卡天梯图,有需要的朋友可以参考参考。2020最新笔记本显卡天梯图以下就是2020年1月..._显卡geforce mx250和gtx1660

随便推点

信息检索笔记-索引构建_为某一文档及集构件词项索引时,可使用哪些索引构建方法-程序员宅基地

文章浏览阅读3.8k次。如何构建倒排索引,我们将这个过程叫做“索引构建”。如果我们的文档很多,这样索引就一次性装不下内存,该如何构建。硬件的限制 我们知道ram读写是随机的操作,只要输入相应的地址单元就能瞬间将数据读出来或者写进去。但是磁盘不行,磁盘必须有一个寻道的过程,外加一个旋转时间。那么只有涉及到磁盘,我们就可以考虑怎么节省I/O操作时间。【注】操作系统往往以数据块为单位进行读写。因为读一_为某一文档及集构件词项索引时,可使用哪些索引构建方法

IT巨头英特尔看好中国市场前景-程序员宅基地

文章浏览阅读836次。英特尔技术与制造事业部副总裁卞成刚7日在财富论坛间隙接受中新社记者采访时表示,该公司看好中国市场前景,扎根中国并以此走向世界是目前最重要的战略之一。卞成刚说,目前该公司正面临战略转型,即从传统PC服务领域扩展至所有智能设施领域,特别是移动终端。而中国目前正引领全球手机市场,预计未来手机、平板电脑等方面的发明创新将大量在中国市场涌现,并推向全球。持相同态度的还有英特尔中国区执行董事戈峻。戈峻

ceph中的radosgw相关总结_radosgw -c-程序员宅基地

文章浏览阅读627次。https://blog.csdn.net/zrs19800702/article/details/53101213http://blog.csdn.net/lzw06061139/article/details/51445311https://my.oschina.net/linuxhunter/blog/654080rgw 概述Ceph 通过radosgw提供RES..._radosgw -c

前端数据可视化ECharts使用指南——制作时间序列数据的可视化曲线_echarts 时间序列-程序员宅基地

文章浏览阅读3.7k次,点赞6次,收藏9次。我为什么选择ECharts ? 本周学校课程设计,原本随机佛系选了一个51单片机来做音乐播放器,结果在粗略玩了CN-DBpedia两天后才回过神,课设还没有开始整。于是懒癌发作,碍于身上还有比赛的作品没交,本菜鸡对硬件也没啥天赋,所以就直接把题目切换成软件方面的题目。写python的同学选择了一个时间序列数据的可视化曲线程序设计题目,果真python在数据可视化这一点性能很优秀。..._echarts 时间序列

ApplicationEventPublisherAware事件发布-程序员宅基地

文章浏览阅读1.6k次。事件类:/** * *   * @className: EarlyWarnPublishEvent *   * @description:数据风险预警发布事件 *   * @param: *   * @return: *   * @throws: *   * @author: lizz *   * @date: 2020/05/06 15:31 * */public cl..._applicationeventpublisheraware

自定义View实现仿朋友圈的图片查看器,缩放、双击、移动、回弹、下滑退出及动画等_imageview图片边界回弹-程序员宅基地

文章浏览阅读1.2k次。如需转载请注明出处!点击小图片转到图片查看的页面在Android开发中很常用到,抱着学习和分享的心态,在这里写下自己自定义的一个ImageView,可以实现类似微信朋友圈中查看图片的功能和效果。主要功能需求:1.缩放限制:自由缩放,有最大和最小的缩放限制 2居中显示:.若图片没充满整个ImageView,则缩放过程将图片居中 3.双击缩放:根据当前缩放的状态,双击放大两倍或缩小到原来 4.单指_imageview图片边界回弹