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》读书笔记 - 5.泛型

    Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...

  2. 清理docker 容器下面的log

    1. docker info 找到docker root dir 2. go to /var/lib/docker 3. constainers 下面有每个容器的文件夹,-json.log 结尾的为L ...

  3. reduce、map、zip、filter使用记录

    注意:结果取完一次就没了: # -*- coding:utf-8 -*- ### functools.reduce from functools import reduce r1 = reduce(l ...

  4. lambda表达式使用解析

    1.Predicate/Consumer/Function/Supplier介绍 Predicate boolean test(T t); Consumer accpet(T t); Function ...

  5. expression,statement,definition ,identifier(symbol) ,literal(字面量) 术语

    expression: an expression evaluates to a value only statement: a statement containing executable cod ...

  6. Eclipse设置保存时自动格式化代码

    在使用eclipse时,经常需要使用到Ctrl+Shift+F来格式化代码,可以打开保存时格式化,会更方便. 打开方式:Window-->Preferences-->Java --> ...

  7. 2018.03.29 python-pandas 数据读取

    #数据读取# read_table,read_csv,read_excel #读取普通分隔数据:read_table #可以读取txt,csv import os import pandas as p ...

  8. input输入框的的input事件和change事件以及change和blur事件的区别

    input输入框的 oninput事件 ,在用户输入的时候触发,只要元素值发生变化就会触发 input输入框的 onchange事件 ,要在输入框失去焦点的时候触发事件,当鼠标在其他地方点击一下才会触 ...

  9. webpack e6转化成es5 配置方法

    方法一: https://www.babeljs.cn/setup#installation 按照babel官方的配置配 方法二: https://www.jianshu.com/p/ce28cedd ...

  10. C#中的委托和事件(一)——delegate

    前言 来说一说委托(delegate)和事件(event),本篇采取的形式是翻译微软Delegate的docs中的重要部分(不要问我为什么微软的docs有中文还要读英文,因为读中文感觉自己有阅读障碍- ...