hdu 6223 Infinite Fraction Path BFS
来源:互联网 发布:别墅外观设计软件 编辑:程序博客网 时间:2024/06/03 20:34
这题做的太闹心了,把vector爆了。。。debug了2个小时。。
虽然vector的maxsize有这么大,我算的也不会爆。
但如果init函数写成如下就会wa,希望路过的能指点下:
save.clear() ; int temp_max = '0' ; for(int i = 0 ; i < n ; i ++ ){ if(read[i] > temp_max) { temp_max = read[i] ; save.clear() ; save.push_back(i) ; }else if(read[i] == temp_max){ save.push_back(i) ; } } while(!q.empty()) q.pop() ; for(int i = 0 ; i < save.size() ; i ++ ){ q.push(node(save[i] , 1)) ; }
思路,由于他是每个结点都能有一个下一个结点既有n条边,n个点,所以每个联通分量一定有环,只要从最大的值开始bfs即可,剪枝:比这层已经搜到的值小的减掉,这层已经搜到这个位置的减掉(即前面的值一样,之后的位置也会一样)
Ac代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 150005 ;char read[maxn] ;struct node{ ll index , step ; node(ll index , ll step){ this->index = index ; this->step = step ; } bool operator < (const node & k ) const { if(step == k.step) return read[index] < read[k.index] ; return step > k.step ; }};priority_queue<node> q ;char ans[maxn] ;bool vis[maxn] ;vector<char> save ;ll sta[maxn] , top ;void init(ll n){ char temp_max = 0 ; top = 0 ; for(ll i = 0 ; i < n ; i ++ ){ if(read[i] > temp_max) { temp_max = read[i] ; top = 0 ; sta[top ++] = i ; }else if(read[i] == temp_max){ sta[top ++] = i ; } } while(!q.empty()) q.pop() ; for(ll i = 0 ; i < top ; i ++ ){ q.push(node(sta[i] , 1)) ; } memset(vis , 0 , sizeof(vis)) ; memset(ans , 0 , sizeof(ans)) ; top = 0 ;}int main(){ //cout << save.max_size() << endl ; ll kase ; scanf("%lld" , &kase) ; ll tt = 1 ; while(kase -- ){ ll n ; scanf("%lld" , &n) ; scanf("%s" , read) ; init(n) ; ll now_step = 1 ; while(!q.empty()){ node t = q.top() ; q.pop() ; if(t.step != now_step){ now_step = t.step ; while(top) vis[sta[--top]] = 0 ; } //int p = ((ll)t.index*(ll)t.index + 1) % (ll)n ; if(ans[t.step] > read[t.index] || vis[t.index] || t.step > n) continue ; vis[t.index] = true ; sta[top ++] = t.index ; ans[now_step] = read[t.index] ; q.push(node((t.index*t.index + 1) % n , t.step + 1)) ; } printf("Case #%lld: " , tt ++ ) ; printf("%s\n" , ans + 1) ; } return 0 ;}
阅读全文
0 0
- HDU 6223 Infinite Fraction Path [bfs+剪枝]
- hdu 6223 Infinite Fraction Path BFS
- HDU 6223 Infinite Fraction Path
- HDU6223-Infinite Fraction Path
- HDU6233 Infinite Fraction Path
- HDU-4775---Infinite Go (bfs)
- 【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}
- SGU 232 Infinite Fraction Hash
- HDU 4775 Infinite Go(暴力模拟+BFS)
- HDU 3760-Ideal Path bfs
- sgu232:Infinite Fraction(最大表示法)
- 2017ACM/ICPC亚洲区沈阳站_Infinite Fraction Path(BFS)
- HDU 3760 Ideal Path 最短路+BFS
- HDU 3760 Ideal Path (bfs,分层)
- hdu 1973 Prime Path (bfs)
- HDU - 1973 - Prime Path (BFS)
- hdu 1973 Prime Path (bfs)
- HDU 4775 Infinite Go
- Fragment中ToolBar问题解析
- 数据结构:链表
- OCR识别中的字符分割
- 解决linux下sudo更改文件权限报错
- 零基础学习java,先从基础书籍入手
- hdu 6223 Infinite Fraction Path BFS
- 机床数据采集的特点
- Spring Boot 中的redis使用
- Search in Rotated Sorted Array
- 算法设计技巧和分析学习笔记1 (归纳法、分治和动态规划)
- 读《从点子到产品》
- struts1配置
- Dialog的进出动画实现
- 机器学习小白如何成长为微软工程师