技术标签: dfs 算法 c++ 树结构 # 算法多校训练 动态规划求解 # ACM特殊算法
什么,你问我维森莫有的题目不能AC,那我告诉你,下面这个骂的就是我!
样例输入
【样例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;
}
样例输入
【样例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;
}
样例输入
【样例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;
}
无根树必须找根。必然有一个点 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;
}
样例输入
【样例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;
}
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;
}
这提示给的,难度直接降到最低…直接分类讨论
#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;
}
文章浏览阅读5.8k次。在大数据的发展当中,大数据技术生态的组件,也在不断地拓展开来,而其中的Hive组件,作为Hadoop的数据仓库工具,可以实现对Hadoop集群当中的大规模数据进行相应的数据处理。今天我们的大数据入门分享,就主要来讲讲,Hive应用场景。关于Hive,首先需要明确的一点就是,Hive并非数据库,Hive所提供的数据存储、查询和分析功能,本质上来说,并非传统数据库所提供的存储、查询、分析功能。Hive..._hive应用场景
文章浏览阅读496次。Zblog是由Zblog开发团队开发的一款小巧而强大的基于Asp和PHP平台的开源程序,但是插件市场上的Zblog采集插件,没有一款能打的,要么就是没有SEO文章内容处理,要么就是功能单一。很少有适合SEO站长的Zblog采集。人们都知道Zblog采集接口都是对Zblog采集不熟悉的人做的,很多人采取模拟登陆的方法进行发布文章,也有很多人直接操作数据库发布文章,然而这些都或多或少的产生各种问题,发布速度慢、文章内容未经严格过滤,导致安全性问题、不能发Tag、不能自动创建分类等。但是使用Zblog采._zblog 网页采集插件
文章浏览阅读2.4k次,点赞2次,收藏2次。restUI页面提交1.1 添加上传jar包1.2 提交任务job1.3 查看提交的任务2. 命令行提交./flink-1.9.3/bin/flink run -c com.qu.wc.StreamWordCount -p 2 FlinkTutorial-1.0-SNAPSHOT.jar3. 命令行查看正在运行的job./flink-1.9.3/bin/flink list4. 命令行查看所有job./flink-1.9.3/bin/flink list --all._flink定时运行job
文章浏览阅读1k次,点赞2次,收藏6次。这个项目是基于STM32的LED闪烁项目,主要目的是让学习者熟悉STM32的基本操作和编程方法。在这个项目中,我们将使用STM32作为控制器,通过对GPIO口的控制实现LED灯的闪烁。这个STM32 LED闪烁的项目是一个非常简单的入门项目,但它可以帮助学习者熟悉STM32的编程方法和GPIO口的使用。在这个项目中,我们通过对GPIO口的控制实现了LED灯的闪烁。LED闪烁是STM32入门课程的基础操作之一,它旨在教学生如何使用STM32开发板控制LED灯的闪烁。_嵌入式stm32闪烁led实验总结
文章浏览阅读63次。本文介绍了安装和部署Debezium的详细步骤,并演示了如何将Debezium服务托管到systemctl以进行方便的管理。本文将详细介绍如何安装和部署Debezium,并将其服务托管到systemctl。解压缩后,将得到一个名为"debezium"的目录,其中包含Debezium的二进制文件和其他必要的资源。注意替换"ExecStart"中的"/path/to/debezium"为实际的Debezium目录路径。接下来,需要下载Debezium的压缩包,并将其解压到所需的目录。
文章浏览阅读4.4k次。需求:在诗词曲文项目中,诗词整篇朗读的时候,文章没有读完会因为屏幕熄灭停止朗读。要求:在文章没有朗读完毕之前屏幕常亮,读完以后屏幕常亮关闭;1.权限配置:设置电源管理的权限。
文章浏览阅读2.3k次。目标检测简介、评估标准、经典算法_目标检测
文章浏览阅读6.3k次,点赞4次,收藏9次。实训时需要安装SQL server2008 R所以我上网上找了一个.exe 的安装包链接:https://pan.baidu.com/s/1_FkhB8XJy3Js_rFADhdtmA提取码:ztki注:解压后1.04G安装时Microsoft需下载.NET,更新安装后会自动安装如下:点击第一个傻瓜式安装,唯一注意的是在修改路径的时候如下不可修改:到安装实例的时候就可以修改啦数据..._sqlserver 127 0 01 无法连接
文章浏览阅读7.4k次。1. Object.keys(item); 获取到了key之后就可以遍历的时候直接使用这个进行遍历所有的key跟valuevar infoItem={ name:'xiaowu', age:'18',}//的出来的keys就是[name,age]var keys=Object.keys(infoItem);2. 通常用于以下实力中 <div *ngFor="let item of keys"> <div>{{item}}.._js 遍历对象的key
文章浏览阅读2.2w次,点赞51次,收藏310次。粒子群算法求解路径规划路径规划问题描述 给定环境信息,如果该环境内有障碍物,寻求起始点到目标点的最短路径, 并且路径不能与障碍物相交,如图 1.1.1 所示。1.2 粒子群算法求解1.2.1 求解思路 粒子群优化算法(PSO),粒子群中的每一个粒子都代表一个问题的可能解, 通过粒子个体的简单行为,群体内的信息交互实现问题求解的智能性。 在路径规划中,我们将每一条路径规划为一个粒子,每个粒子群群有 n 个粒 子,即有 n 条路径,同时,每个粒子又有 m 个染色体,即中间过渡点的_粒子群算法路径规划
文章浏览阅读353次。所谓稳健的评估指标,是指在评估的过程中数据的轻微变化并不会显著的影响一个统计指标。而不稳健的评估指标则相反,在对交易系统进行回测时,参数值的轻微变化会带来不稳健指标的大幅变化。对于不稳健的评估指标,任何对数据有影响的因素都会对测试结果产生过大的影响,这很容易导致数据过拟合。_rar 海龟
文章浏览阅读607次,点赞2次,收藏7次。–基于STM32F103ZET6的UART通讯实现一、什么是IAP,为什么要IAPIAP即为In Application Programming(在应用中编程),一般情况下,以STM32F10x系列芯片为主控制器的设备在出厂时就已经使用J-Link仿真器将应用代码烧录了,如果在设备使用过程中需要进行应用代码的更换、升级等操作的话,则可能需要将设备返回原厂并拆解出来再使用J-Link重新烧录代码,这就增加了很多不必要的麻烦。站在用户的角度来说,就是能让用户自己来更换设备里边的代码程序而厂家这边只需要提供给_value line devices connectivity line devices