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. mysql update语句与limit的结合使用

    有时候有需要批量更新数据表中从多少行到多少行的某个字段的值 mysql的update语句只支持更新前多少行,不支持从某行到另一行,比如 UPDATE tb_name SET column_name=' ...

  2. Electron-Vue工程初始化,以及需要掌握的相关知识

    1.安装nodejs 下载地址:http://nodejs.cn/ 需要重启系统 2.安装electron npm install electron -g 3.安装vue npm install vu ...

  3. linux可用的跨平台C# .net standard2.0 写的高性能socket框架

    能在window(IOCP)/linux(epoll)运行,基于C# .net standard2.0 写的socket框架,可使用于.net Framework/dotnet core程序集,.使用 ...

  4. leetcode-mid-backtracking -22. Generate Parentheses-NO

    mycode    没有思路,大早上就有些萎靡,其实和上一个电话号码的题如出一辙啦 参考: class Solution(object): def generateParenthesis(self, ...

  5. Spring mvc注解说明

    编号 注解 说明 位置 备注 1 @Controller 将类变成Spring Bean 类 现阶段 @Controller . @Service 以及 @Repository 和 @Componen ...

  6. 阶段3 1.Mybatis_02.Mybatis入门案例_2.mybatis入门案例中的设计模式分析

    读取配合文件 创建工厂 最终图

  7. C#与Java覆盖问题

    C#继承类中如含有相同的方法,避免冲突使用new关键字.在不同对象中分别对应该方法.若使用override关键字则,基类中的方法被覆盖. 如需调用非覆盖的则使用base关键字. Java中的继承类方法 ...

  8. window.screenLeft&&window.screenTop&&window.screenX&&window.screenY

    http://blog.sina.com.cn/s/blog_14e2a237b0102w4i0.html window.screenLeft&&window.screenTop&am ...

  9. javascript 访问 webservice

    xml: <?xml version="1.0" encoding="UTF-8"?> <boolean xmlns="http:/ ...

  10. JavaScript —— 常见用途

    javaScript 简介 第一个JavaScript 程序: 点击按钮显示日期   <!DOCTYPE html> <html> <head> <meta ...