HDU 5305 Friends DFS+剪枝
来源:互联网 发布:sql server 2017 sn 编辑:程序博客网 时间:2024/06/05 06:57
题意:n个人,m对朋友,每一对朋友可以是online friend或者offline friends,m对朋友不重复,每个人的两种朋友数量必须相等,问有多少种方法满足要求
方法:注意:朋友的朋友和自己没有关系,用DFS回溯+剪枝,测试每对朋友为online friend和offline friends的情况,当任一个人的朋友数为奇数时候可以直接输出0因为分配不
了, 当一个人的两种朋友都满了的时候可以return。
#include <stdio.h>#include <deque>#define maxn 100005using namespace std;int a[maxn];int f(int n){ return (n + n * (n - 1) / 2);}int main(){ int l, r, t, n, k, i; long long int ans; scanf("%d", &t); while(t--) { scanf("%d %d", &n, &k); deque<int> q1; deque<int> q2; for(i = 1;i <= n;i++) scanf("%d", &a[i]); l = r = 1; ans = 0; while(r <= n) { while(!q1.empty()&&a[r] < q1.back()) q1.pop_back(); q1.push_back(a[r]); while(!q2.empty()&&a[r] > q2.back()) q2.pop_back(); q2.push_back(a[r]); while(!q1.empty()&&!q2.empty()&&q2.front() - q1.front() >= k) { if(q1.front() == a[l]) q1.pop_front(); if(q2.front() == a[l]) q2.pop_front(); l++; } ans += (r - l + 1); r++; } printf("%I64d\n", ans); }}
0 0
- *HDU 5305 - Friends (DFS + 剪枝)
- HDU 5305 Friends DFS+剪枝
- HDU 5305 Friends(DFS + 剪枝)
- HDU 5305 Friends(2015多校第二场 dfs + 剪枝)
- HDU 5305 Friends (DFS)
- HDU 5305 Friends (DFS)
- hdu 5305 Friends (dfs)
- HDU 5305 Friends DFS
- hdu 5305 Friends dfs
- HDU 5305 Friends(DFS)
- HDU 5305 Friends(dfs)
- Friends(DFS+剪枝)
- hdu 5305 Friends(搜索+剪枝)
- HDU 5305 Friends(dfs)
- HDU 5305 Friends(dfs)
- hdu 5305 Friends(dfs)
- HDU 5305 Friends(简单DFS)
- hdu(5305)——Friends(dfs)
- Visual C#2010学习笔记十之类的创建和调用
- 什么会在main函数之前执行
- getResource于getResourceAsStream的使用
- 浅析系统性能调优
- Scala学习第八天 Scala主构造器、私有构造器、构造器重载实战详解
- HDU 5305 Friends DFS+剪枝
- POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
- ural 1008 Image Encoding
- Hiberante中的QBC检索方式
- COCOS2D-X项目在XCODE没有IOS Simulator,无法仿真的解决办法
- 使 spine MVC 支持 X-HTTP-Method-Override
- 浅析性能测试培训总结-IP欺骗
- 动态代理
- [POJ 1308]Is It A Tree?[并查集][判断有向树]