C - Not so Diverse

D - Non-decreasing

先找绝对值最大的数 构造出全正(最大的数为正) 或者全负(最大的数为负)

然后前缀和(正)或者后缀和(负) 操作次数2n-2

#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
map<int, int> mp;
int num[];
int main()
{
//freopen("out1.txt", "w", stdout);
int ans = -;
int aim;
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
scanf("%d", &num[i]);
if (abs(num[i]) > ans)
{
ans = abs(num[i]);
aim = i;
}
}
if (num[aim] == )
{
cout << << endl;
}
else if (num[aim] > )
{
cout << * n - << endl;
for (int i = ; i <= n; i++)
{
if (i == aim)
{
continue;
}
cout << aim << " " << i << endl;
}
for (int i = ; i < n; i++)
{
cout << i << " " << i + << endl;
}
}
else
{
cout << * n - << endl;
for (int i = ; i <= n; i++)
{
if (i == aim)
{
continue;
}
cout << aim << " " << i << endl;
}
for (int i = n; i > ; i--)
{
cout << i << " " << i - << endl;
}
} }

!!!!!E - Smuggling Marbles

【题意】

给定n+1个点的树(root=0),每个点可以选择放或不放弹珠,每一轮顺序进行以下操作:

1.将根节点0的弹珠加入答案。

2.每个点的弹珠移向父亲。

3.如果一个点有超过2个弹珠,全部丢掉。

如果树中仍有弹珠,继续下一轮。

共有2^(n+1)种放弹珠的方案,计算所有方案的答案之和,取模1e9+7。

n<=2*10^5。(部分分:n<=2*10^3)

【题解】

容易发现,层与层之间互相独立,第i轮只需要考虑第i层的节点组合的子集中有多少个子集能到达0点,加起来就是总答案。

接下来考虑每轮进行一次树形DP,为了方便求解集合交,将方案计算转化为概率计算(集合交就是概率的乘积),则每个点有弹珠的概率是1/2。

令f[i][j]表示节点i有j个弹珠(j=0,1)的概率,则有:

f[i][1]=Σs/f[j][0]*f[j][1],s=Πf[j][0],j=son(i)

f[i][0]=1-f[i][1]

每一轮将对应深度的点全部初始化为1/2,然后树形DP到根就可以得到答案,复杂度O(n^2)

考虑将一个点在多轮的情况都考虑起来,f[i][d][j]表示点i在第d轮有j个弹珠的概率(j=0,1,2,2代表>=2)。

令f[i][d]={f[i][d][0],f[i][d][1],f[i][d][2]},即视为一个状态,对于同轮(同深度同d)的两个状态可以合并(两个状态对应9种交集,交集乘 后 并集加)

对于一个点要将其所有儿子合并,两个点合并只需将0~min(d1,d2)的状态对应合并,以d大的点作为基础来合并(不要复制)。

那么初始状态为f[i][0]={1/2,1/2,0},对于每个点将其儿子全部合并,然后顺推一位将d=0设为初始状态,最后记得将状态中的2搬到0处,注意这个过程必须只搬有改动的状态才能保证复杂度(之前不能直接归为0是因为在儿子的合并中2和0有区别)

复杂度分析同线段树合并,O(n)。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = , MOD = ;
int read()
{
char c;
int s = , t = ;
while (!isdigit(c = getchar()))if (c == '-')
{
t = -;
}
do
{
s = s * + c - '';
}
while (isdigit(c = getchar()));
return s * t;
}
struct cyc
{
int z0, z1, z2;
};
cyc operator + (cyc a, cyc b)
{
cyc c;
c.z0 = 1ll * a.z0 * b.z0 % MOD;
c.z1 = (1ll * a.z0 * b.z1 + 1ll * a.z1 * b.z0) % MOD;
c.z2 = (1ll * a.z0 * b.z2 + 1ll * a.z2 * b.z0 + 1ll * a.z1 * b.z1 + 1ll * a.z2 * b.z2 + 1ll * a.z1 * b.z2 + 1ll * a.z2 * b.z1) % MOD;
return c;
}
vector<cyc>a[maxn];
int n, fa[maxn], first[maxn], cnt = , tot = , b[maxn];
struct edge
{
int v, from;
} e[maxn * ];
void insert(int u, int v)
{
cnt++;
e[cnt].v = v;
e[cnt].from = first[u];
first[u] = cnt;
}
int MO(int x)
{
return x >= MOD ? x - MOD : x;
}
void merge(int &x, int y)
{
if (a[x].size() < a[y].size())
{
swap(x, y);
}
for (int i = ; i < (int)a[y].size(); i++)
{
a[x][a[x].size() - i - ] = a[x][a[x].size() - i - ] + a[y][a[y].size() - i - ];
}
}
int main()
{
n = read() + ;
for (int i = ; i <= n; i++)
{
fa[i] = read() + , insert(fa[i], i);
}
for (int i = n; i >= ; i--)
{
int mx = ;
if (!first[i])
{
a[b[i] = ++tot].push_back((cyc)
{
(MOD + ) / , (MOD + ) / ,
});
}
else
{
b[i] = b[e[first[i]].v];
for (int j = e[first[i]].from; j; j = e[j].from)
{
mx = max(mx, min((int)a[b[i]].size(), (int)a[b[e[j].v]].size()));
merge(b[i], b[e[j].v]);
}
a[b[i]].push_back((cyc)
{
(MOD + ) / , (MOD + ) / ,
});
}
for (int j = (int)a[b[i]].size() - - ; j >= (int)a[b[i]].size() - mx - ; j--)
{
a[b[i]][j].z0 += a[b[i]][j].z2, a[b[i]][j].z2 = ;
}
}
int ans = , N = ;
for (int i = ; i <= n; i++)
{
N = (N << ) % MOD;
}
for (int i = ; i < (int)a[b[]].size(); i++)
{
ans = MO(ans + 1ll * a[b[]][i].z1 * N % MOD);
}
printf("%d", ans);
return ;
}

AT Regular 086的更多相关文章

  1. AtCoder Regular Contest 086 E - Smuggling Marbles(树形迭屁)

    好强的题. 方案不好算,改成算概率,注意因为是模意义下的概率所以直接乘法逆元就好不要傻傻地开double. 设$f[i][d][0]$为第i个节点离d层的球球走到第i个点时第i个点没有球的概率, $f ...

  2. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. MongoVUE1.6.9破解启动提示System.ArgumentException: 字体“Courier New”不支持样式“Regular”

    用MongoVUE,发现报错,报错信息如下: System.ArgumentException: 字体"Courier New"不支持样式"Regular". ...

  4. myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc

    今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...

  5. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  6. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  7. scp报错:not a regular file,解决方法:加参数 -r

    命令:scp  -P1234  /data/aa   root@192.0.0..0:/data 文件结构:/data/aa/yearmonth=2015-09 报错:not a regular fi ...

  8. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. glyphicon halflings regular ttf 报错

    一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...

随机推荐

  1. 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法

    Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...

  2. JSON.stringify方法报错:Converting circular structure to JSON

    别以为JSON.parse(JSON.stringify(data))做深拷贝无敌,对于以下这种情况,当你需要保留父级对象,即 对象存在循环引用,就会报错. var a = [ { "id& ...

  3. Jupiter Code Review Reference -- Jupiter代码审查工具使用参考

    Jupiter Code Review Reference -- Jupiter代码审查工具使用参考 (修改版) 原创 2010年07月06日 10:43:00 标签: 审查 / reference  ...

  4. 条形码(barcode)code128生成代码

    条形码(barcode)code128生成代码 很简单 多些这位兄弟https://bbs.csdn.net/topics/350125614 下面是我的DEMO 直接放到VS2005下面编译即可 # ...

  5. leetcode 287寻找重复数

    这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: class Solution { public: int findDuplicate(vec ...

  6. 歌手详情数据处理和Song类的封装

    我们现在每首歌曲的数据都是这样的 我们需要在这个数据里面去提取我们需要的部分,来构造成我们需要的数据对象 那我们要和创建singer.js一样  同样也要创建song.js类 我们还要获取到每首歌对应 ...

  7. TP3.2写分页

    用TP3.2写分页 手册上说的好难懂,我自己去网上找资料 ,现在整理一下,以后可能会用: 在Think下面有Page.class.php类: 我在这个下面放了一个function.php的(算是类吧又 ...

  8. python中的装饰器练习

    一:编写装饰器,为多个函数加上认证的功能(用户的账号密码) 要求登录成功一次,后续的函数都无需输入用户名和密码FLAG=False#此时还未登录 全局变量 写这个步骤的意义在于:方便 知道已经登录成功 ...

  9. 关于Polyaxon的使用

    1.首先upload代码 polyaxon upload 注意,upload只能在新建notebook前进行,且每次只能upload一次,所以在结束每次项目测试(开发)前,都应该在Polyaxon上修 ...

  10. win10安装Tensorflow1.9GPU版本

    前言 看到DateWhale出了一篇安装教程(微信公众号DateWhale),决定体验一下Tensorflow1.9的GPU版本..其实一开始装的是2.0,但是tf.Session()就报错了,说是2 ...