zoukankan      html  css  js  c++  java
  • Daliy Algorithm (dp,数学,贪心)-- day 78

    Nothing to fear


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

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

    2020.5.14


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

    lc-面试题63. 股票的最大利润

    找到最小得哪个点买入,每次遇到一个比它价格高得就计算和之前相比所能得到得最大利润

    class Solution:
        def maxProfit(self, prices: List[int]) -> int:
            if len(prices) == 0:
                return 0
            price , profit = prices[0] , 0
            for i in range(len(prices)):
                if prices[i] < price:
                    price = prices[i]
                else:
                    profit = max(prices[i] - price,profit)
            return profit
    

    lc-523. 连续的子数组和

    我还以为我想到得(O(n^{2}))的解法也过不了呢 不得不说评测鸡真好 10^8搁在别的地方大概率是通过不了的吧,就这题通过率还这么低,不过还是有一些小坑点

    class Solution:
        def checkSubarraySum(self, nums: List[int], k: int) -> bool:
            n = len(nums)
            if n == 0:
                return False
            prefix = [0 for _ in range(n)]
            prefix[0] = nums[0]
            for i in range(1,n):
                prefix[i] = prefix[i-1] + nums[i]
            for i in range(n):
                for j in range(i+1,n):
                    sumc = prefix[j]-prefix[i]+nums[i]
                    if sumc == k or (k != 0 and sumc % k == 0):
                        return True
            return False
    
    

    对于前三题做题慢得主要原因就是读题太慢了 还老是读错题意,我这蹩脚得英语 唉~
    剩下得后面补题吧 太困了

    CF642-A

    思维

    
    #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;
    
    void slove()
    {
        int n , m;
        cin >> n >> m;
        if(n == 1)
        {
            cout << 0 << endl;
        }
        if(n == 2)
        {
            cout << m << endl;
        }
        if(n >= 3)
        {
            cout << 2 * m << endl;
        }
        return ;
    }
    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
    }
    

    B

    排序 , 贪心

    #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;
    
    void slove()
    {
        int n , k;
        cin >> n >> k;
        vector<int> a(n + 1) , b(n + 1);
        for(int i = 1;i <= n ;i ++)cin >> a[i];
        for(int i = 1;i <= n ;i ++)cin >> b[i];
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        ll sum = 0;
        int i = 1 ,j = n;
        while(k--)
        {
            if(a[i] < b[j])
            {
                swap(a[i],b[j]);
                i++;j--;
            }
        }
        for(i = 1; i <= n ;i ++)sum += a[i];
        cout << sum << 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
    }
    

    C

    数学

    #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;
    
    void slove()
    {
        int n;
        cin >> n;
        ll ans = 0;
        for(int i = 1;i <= n / 2; i ++)
        {
            ans += i * 1ll * i;
        }
        cout << ans * 8 << 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
    }
    
  • 相关阅读:
    Java程序语言的后门-反射机制
    JAVA设计模式-单例模式(Singleton)线程安全与效率
    JAVA设计模式-动态代理(Proxy)源码分析
    Mybatis源码解析,一步一步从浅入深(七):执行查询
    Mybatis源码解析,一步一步从浅入深(六):映射代理类的获取
    python程序中的进程操作
    进程
    操作系统的发展史
    详尽实用的 PyCharm 教程
    python的socketserver模块实现TCP/UDP并发
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12893195.html
Copyright © 2011-2022 走看看