zoukankan      html  css  js  c++  java
  • Codeforces 68思路(rng_58版)

    申明:本文所有思路均为本人自己观摩大神代码思考得出,如有不符,望诸位大佬不要怪罪。
    A. Irrational problem
    代码:

    using namespace std;
     
    #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
    #define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
     
    int main(void){
        int i,a,b,p1,p2,p3,p4,ans=0;
        
        cin >> p1 >> p2 >> p3 >> p4 >> a >> b;
        for(i=a;i<=b;i++) if(i < p1 && i < p2 && i < p3 && i < p4) ans++;
        cout << ans << endl;
        
        return 0;
    }
    

    思路:这一道题的思路十分明显,就是暴力枚举判断即可。
    B. Energy exchange
    代码:

    using namespace std;
     
    #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
    #define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
     
    int a[10010];
     
    int main(void){
        int N,i,j,tmp;
        double p;
        
        scanf("%d%d",&N,&tmp);
        p = 1.0 - tmp / 100.0;
        REP(i,N) scanf("%d",&a[i]);
        
        double high = 2000.0, low = 0.0, mid;
        REP(i,100){
            mid = (high + low) / 2.0;
            double sum = 0.0;
            REP(j,N) if(a[j] > mid) sum += (a[j] - mid) * p; else sum -= (mid - a[j]);//加上差值
            if(sum > 0.0) low = mid; else high = mid;//判断
        }
        
        printf("%.9f
    ",mid);
        
        return 0;
    }
    

    思路:这一道题,是一个二分题,但是rng_58这种写法是预先算好最多步骤再进行处理的。判断很简单。
    C. Synchrophasotron

    using namespace std;
     
    #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
    #define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
     
    #define INF (1<<29)
     
    int f[10][10];
    int N,low[10][10],high[10][10],a[10][10];
    int minflow,maxcost;
     
    void dfs(int x, int y){
        int i,j,cur;
        
        if(x == -1){
            int flow = 0;
            REP(i,N-1) flow += f[i][0];
            if(flow > minflow) return;
            
            int cost = 0;
            REP(i,N-1) REP(j,i+1) if(f[i][j] > 0) cost += a[i][j] + f[i][j] * f[i][j];
            
        //  REP(i,N-1) REP(j,i+1) cout << f[i][j] << ' '; cout << endl;
            
            if(flow < minflow || (flow == minflow && cost > maxcost)) {minflow = flow; maxcost = cost;}
            return;
        }
        
        for(cur=low[x][y];cur<=high[x][y];cur++){
            f[x][y] = cur;
            
            if(y == 0){
                int sum = 0;
                REP(i,x+1) sum += f[x][i];
                for(i=x+1;i<N-2;i++) sum -= f[i][x+1];
                
                if(sum < low[N-2][x+1] || sum > high[N-2][x+1]) continue;
                f[N-2][x+1] = sum;
                dfs(x-1,x-1);
            } else {
                dfs(x,y-1);
            }
        }
    }
     
    int main(void){
        int i,s,f_;
        
        scanf("%d",&N);
        REP(i,N*(N-1)/2){
            scanf("%d%d",&s,&f_); s--; f_ -= 2; swap(s,f_);
            scanf("%d%d%d",&low[s][f_],&high[s][f_],&a[s][f_]);
        }
        
        minflow = INF; maxcost = -INF;
        f[N-2][0] = low[N-2][0];
        dfs(N-3,N-3);
        
        if(minflow == INF){
            printf("-1 -1
    ");
        } else {
            printf("%d %d
    ",minflow,maxcost);
        }
        
        return 0;
    }
    

    思路:一道看似最大流,实际上是DFS的题目,用DFS找到每一个的花费和流量,然后再综合到一起求最小流量、最大花费。
    D. Half-decay tree

    using namespace std;
     
    #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
    #define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
     
    int H;
    map <int, int> cost,sum;
    char buf[100];
     
    void add(int v, int e){
        cost[v] += e;
        while(v >= 1){
            sum[v] += e;
            v /= 2;
        }
    }
     
    double func(int depth, int x, double big){
        if(depth == H){
            if(cost.find(x) == cost.end()) return big;
            double ans = cost[x];
            if(big > ans) ans = big;
            return ans;
        } else {
            double cur = cost[x], L = sum[2*x], R = sum[2*x+1];
            double ans1 = max(big,cur+L);
            if(R > cur+L+0.5 && R > ans1) ans1 = func(depth+1,2*x+1,ans1);
            double ans2 = max(big,cur+R);
            if(L > cur+R+0.5 && L > ans2) ans2 = func(depth+1,2*x,ans2);
            return (ans1 + ans2) / 2.0;
        }
    }
     
    int main(void){
        int Q,i,v,e;
        
        scanf("%d%d",&H,&Q);
        REP(i,Q){
            scanf("%s",buf);
            if(buf[0] == 'a'){
                scanf("%d%d",&v,&e);
                add(v,e);
            } else {
                double ans = func(0,1,0.0);
                printf("%.9f
    ",ans);
            }
        }
        
        return 0;
    }
    

    思路:这是一道概率DP。
    修改操作:由于修改操作至多(q)次,涉及到的树上节点至多(q cdot h)个,而查询是查询连通分支的权值和,故存储以该点为根节点的子树权值和,更新直接加即可。
    查询操作:从根节点开始深搜,假设当前在(u)节点,已经固定的连通分支权值和最大值为(mx)(u)节点到两个儿子的边必然断一条,对于分离出去的子树继续深搜,另一棵子树与(u)节点以及其他未固定节点构成新的连通分支,用该连通分支的权值和更新(mx),由于当前只有两种方案,故期望值即为这两种方案的答案之和除以(2)

    总结与收获:这4道题,除了第一道之外,后面几道题我的算法都有不足的地方,例如第二题,rng_58的写法容错率较高,不容易被hack,并且修改起来较简单。而后面的两题,rng_58的算法就更为明确,相比较我的而言,很多地方更加明朗,更方便思考,也更不容易出错。

  • 相关阅读:
    正则表达式
    什么是面向对象
    关于jdk,jre,jvm和eclipse的一些总结
    分析ajax爬取今日头条街拍美图
    pycharm误删恢复方法及python扩展包下载地址
    django 之 视图层、模板层
    django
    django框架基础二
    jdango框架基础一
    安装软件,提高速度,可以使用清华源
  • 原文地址:https://www.cnblogs.com/withhope/p/11148212.html
Copyright © 2011-2022 走看看