zoukankan      html  css  js  c++  java
  • 2020-09-24 刷题记录

    Aerodynamic

    思路:

    判断一个图形是否是中心对称图形。

    • (n) 是奇数,显然不行。
    • (n) 是偶数。找出对应点对的中心。如果是对称的,那么一定会相交于一点。

    代码:

    int n; cin >> n;
    vector<PII> point(n + 1);
    vector<PDD> center(n + 1);
    for(int i = 1; i <= n; i ++) scanf("%d %d", &point[i].x, &point[i].y);
    if(n & 1) { puts("NO"); return 0; }
    for(int i = 1; i <= n / 2; i ++){
        center[i].x = (point[i].x + point[i + n / 2].x) * 0.5;
        center[i].y = (point[i].y + point[i + n / 2].y) * 0.5;
    }
    
    int mark = 1;
    for(int i = 1; i <= n / 2; i ++){
        if(center[i].x == center[1].x && center[i].y == center[1].y) continue;
        else { mark = 0; break; }
    }
    if(mark == 1) puts("YES");
    else puts("NO");
    

    Fast Food Restaurant

    思路:

    暴力出奇迹

    代码:

    auto calc = [&] (int x, int y, int z) -> int {
       int res = 0;
       if(x) { res ++; x --; }
       if(y) { res ++; y --; }
       if(z) { res ++; z --; }
       if(x && y) { res ++; x --; y --; }
       if(x && z) { res ++; x --; z --; }
       if(y && z) { res ++; y --; z --; }
       if(x && y && z) { res ++; }
       return res;
    };
    
    int t; cin >> t;
    while(t --){
       vector<int> a(3);
       cin >> a[0] >> a[1] >> a[2];
       sort(all(a));
       int ans = calc(a[0], a[1], a[2]);
       while(next_permutation(all(a))) 
           ans = max(ans, calc(a[0], a[1], a[2]));
       cout << ans << endl;
    }
    

    501. 二叉搜索树中的众数

    思路:

    中序遍历得到的序列就是递增的,所以中序遍历一遍即可。注意边界的判断。

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        
        vector<int> res;
        int cnt = 0; int maxCnt = 0, last = -1;
        
        inline void dfs (TreeNode * node){
                
            if(node == NULL) return;
            dfs(node->left);
            if(last == node->val) cnt ++;
            else{
                if(last == -1);
                else if(cnt > maxCnt) { maxCnt = cnt; res.clear(); res.push_back(last); }
                else if(cnt == maxCnt) { res.push_back(last); }
                last = node->val;
                cnt = 1;
            }
            dfs(node->right);
        }
        
        vector<int> findMode(TreeNode* root) {
            
            dfs(root);
            if(cnt > maxCnt) { res.clear(); res.push_back(last); }
            if(cnt == maxCnt && cnt) { res.push_back(last); }
            return res;
    
        }
    };
    
  • 相关阅读:
    HDU1029 Ignatius and the Princess IV
    UVA11039 Building designing【排序】
    UVA11039 Building designing【排序】
    POJ3278 HDU2717 Catch That Cow
    POJ3278 HDU2717 Catch That Cow
    POJ1338 Ugly Numbers(解法二)
    POJ1338 Ugly Numbers(解法二)
    UVA532 Dungeon Master
    UVA532 Dungeon Master
    POJ1915 Knight Moves
  • 原文地址:https://www.cnblogs.com/nonameless/p/13727586.html
Copyright © 2011-2022 走看看