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. legend3---OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054

    legend3---OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 一.总结 一句话总结: 解决方法:多试几次,实在不行就手动下载 1.homeste ...

  2. ppapi,npapi

    PPAPI也就是Pepper Plugin API,是在原有网景NPAPI(Netscape Plugin API)基础上发展而来的.NPAPI是当今最流行的插件架构,几乎所有浏览器都支持,不过存在很 ...

  3. MyRocks安装部署

    参考:https://www.cnblogs.com/WonderHow/p/5621591.html CentOS 7.3 gflags:git clone https://github.com/g ...

  4. linux shell 中"2>&1"含义-完美理解-费元星

    笨鸟先飞,先理解.   脚本是:      nohup /mnt/Nand3/H2000G  >/dev/null  2>&1  &      对于& 1 更准确的 ...

  5. 在项目中配置PageHelper插件时遇到类型转换异常

    PageHelper是一种常用的分页工具,按照常规方法在mybatis的配置文件中整合它: <?xml version="1.0" encoding="UTF-8& ...

  6. 3D聚类

    1 3D聚类和普通的二维聚类实质一样,只不过维数太高了,用三维图来表示了. 下面将官网的改成只生成一个图了 #!/usr/bin/python # -*- coding:utf-8 -*- print ...

  7. C# 实现IDisposable

    #region 实现IDisposable public void Dispose() { Dispose(true); GC.SuppressFinalize(this);//防止Finalize调 ...

  8. 通俗易懂的理解 Redux(知乎)

    1. React有props和state: props意味着父级分发下来的属性[父组件的state传递给子组件  子组件使用props获取],state意味着组件内部可以自行管理的状态,并且整个Rea ...

  9. 【FICO系列】SAP FICO 凭证错误:BKPFF$PRDCLN800在FI中达到的项目最大编号

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO 凭证错误:BK ...

  10. Win密钥.Win7旗舰版

    1.windows7旗舰版免费密钥 - Win7之家.html(http://www.windows7en.com/Win7/25762.html) HT6VR-XMPDJ-2VBFV-R9PFY-3 ...