hdu 6223 Infinite Fraction Path BFS


这题做的太闹心了,把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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值