zoukankan      html  css  js  c++  java
  • [NOI2013]树的计数

    // luogu-judger-enable-o2
    /*
    考虑将bfs序按层分段, 每分一段就会使深度+1,所以分的段数+1就是深度
    由于每种分段方式至多只能对应一种dfs序, 所以我们的目标就是求出可行的bfs序
    然后我们发现, 如果在bfs序中第i个比第i + 1个后出现在dfs序中, 那么这里一定分段
    
    然后我们考虑dfs序对于bfs序的限制, 假设a[i] < a[i + 1]的话,意味着a[i + 1]和a[i]同层或者在下一层
    
    那么sum_{i = a[i]} ^ {a[i + 1] - 1} 分层<= 1
    
    这样的话得到了一些限制以及确定位置, 没有限制的位置因为任意分都可以, 那么统计0.5答案即可
     
    
    
    */
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #define mmp make_pair
    #define ll long long
    #define M 200010
    using namespace std;
    int read() {
    	int nm = 0, f = 1;
    	char c = getchar();
    	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    	for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    	return nm * f;
    }
    double note[M], s[M];
    int  t[M], a[M], pos[M], f[M], sta[M], n, tp;
    int main() {
    	double ans = 1;
    	n = read();
    	for(int i = 1; i <= n; i++) t[read()] = i;
    	for(int i = 1; i <= n; i++) {
    		int x = read();
    		a[t[x]] = i, pos[i] = t[x];
    	}
    	note[1] = 1;
    	for(int i = 1; i < n; i++) {
    		if(pos[i] > pos[i + 1]) note[i] = 1;
    		if(note[i] == 1) {
    			f[i]++;
    			f[i + 1]--;
    		}
    		s[i] = s[i - 1] + note[i];
    	}
    	for(int i = 1; i < n; i++) {
    		if(a[i] < a[i + 1]) {
    			if(s[a[i + 1] - 1] - s[a[i] - 1] > 0) {
    				f[a[i]]++;
    				f[a[i + 1]]--;
    			} else sta[++tp] = a[i];
    		}
    	}
    	for(int i = 1; i <= n; i++) f[i] += f[i - 1];
    	for(int i = 1; i <= tp; i++) {
    		if(f[sta[i]] == 0) note[sta[i]] = 0.5;
    	}
    	for(int i = 1; i <= n; i++) ans += note[i];
    	printf("%.3lf
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    IE不支持 ES6 Promise 对象的解决方案
    微信小程序使用阿里图标
    IE浏览器 ajax传参数值为中文时出现乱码的解决方案
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
    常见的一些浏览器兼容问题
    移动端rem设置(部分安卓机型不兼容)
    element ui el-menu样式调整
    原生login页面
    elemet ui去除table 样式
    项目上线
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/10903784.html
Copyright © 2011-2022 走看看