zoukankan      html  css  js  c++  java
  • Daliy Algorithm (数学,堆,dfs,贪心)-- day 82

    Nothing to fear


    种一棵树最好的时间是十年前,其次是现在!

    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.5.18


    人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!

    PAT-A1147 Heaps

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <cmath>
    #include <cstring>
    
    using namespace std;
    const int N = 10005;
    int a[N];
    int m , t;
    bool Max = false , Min = false;
    int preorder[N];
    int cnt = 2;
    void dfs(int now)
    {
        int ls = now * 2;
        int rs = now * 2 + 1;
        if(now > m)return;
        if(ls <= m)
        {
            if(a[now] > a[ls])Max = 1;
            else Min = 1;
            dfs(ls);
        }
        if(rs <= m)
        {
            if(a[now] > a[rs])Max = 1;
            else Min = 1;
            dfs(rs);
        }
        preorder[cnt++] = a[now];
    }
    void slove()
    {
        for(int i = 1;i <= m ;i ++)scanf("%d",&a[i]);
        dfs(1);
        if(Min  && Max)cout << "Not Heap" << endl;
        if(Max  &&!Min)cout << "Max Heap" << endl;
        if(!Max && Min)cout << "Min Heap" << endl;
        for(int i = 1;i <= m ;i ++)
        {
            if(i == m)printf("%d
    ",preorder[i]);
            else printf("%d ",preorder[i]);
        }
        return;
    }
    void init()
    {
        Max = 0,Min = 0;
        memset(a , 0 , sizeof a);
        memset(preorder , 0 , sizeof preorder);
        cnt = 1;
    }
    int main()
    {
        cin >> t >> m;
        while(t--)
        {
            init();
            slove();
        }
        return 0;
    }
    

    Ternary String

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    #include <vector>
    #include <cassert>
    #include <string>
    #include <iomanip>
    #include <cmath>
    #include <ctime>
    #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    #define lowbit(x) (x & -x)
    #define npos string::npos
    using namespace std;
    typedef long long ll;
    const int MAX = 0x7ffffff;
    int t;
    
    void slove()
    {
        string s;
        cin >> s;
        int n = s.size();
        if(s.find('1') == npos || s.find('2') == npos || s.find('3') == npos)
        {
            cout << 0 << endl;
            return ;
        }
        int a = -1, b = -1 , c = -1 , ans = n + 1;
        for(int i = 0;i < n;i ++)
        {
            if(s[i] == '1')a = i;
            if(s[i] == '2')b = i;
            if(s[i] == '3')c = i;
            //printf("%d %d %d
    ",a , b ,c);
            if(a != -1 && b != -1 && c != -1)
            {
                int x = max(max(a, b) , c);
                int y = min(min(a ,b),  c);
                ans = min(abs(y - x) + 1,ans);
            }
        }
        cout << ans << endl;
    }
    int main()
    {
    #ifdef LOCAL
        auto start_time = clock();
        cerr << setprecision(3) << fixed; // 在iomanip中
    #endif
        SIS;
        cin >> t;
        while(t--)
        {
            slove();
        }
    #ifdef LOCAL
        auto end_time = clock();
        cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms
    ";
    #endif
    }
    

    Simple Polygon Embedding

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    #include <vector>
    #include <cassert>
    #include <string>
    #include <iomanip>
    #include <cmath>
    #include <ctime>
    #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    #define lowbit(x) (x & -x)
    using namespace std;
    typedef long long ll;
    const int MAX = 0x7ffffff;
    int t;
    const double PA = 3.14159265358979323846;
    void slove()
    {
        int n;
        cin >> n;
        printf("%.9f
    ", 1 / tan(PA / (2 * n)));
    }
    int main()
    {
    #ifdef LOCAL
        auto start_time = clock();
        cerr << setprecision(3) << fixed; // 在iomanip中
    #endif
        SIS;
        cin >> t;
        while(t--)
        {
            slove();
        }
    #ifdef LOCAL
        auto end_time = clock();
        cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms
    ";
    #endif
    }
    
  • 相关阅读:
    轻松搭建Redis缓存高可用集群
    Redis集群主从配置
    启动Redis Cluster
    MyISAM 和 InnoDB 索引的区别
    数据库面试
    如何定位php程序访问慢
    Socket技术详解
    NGINX快速入门
    nginx 并发数问题思考:worker_connections,worker_processes与 max clients
    php-fpm运行原理
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12913663.html
Copyright © 2011-2022 走看看