zoukankan      html  css  js  c++  java
  • 2020/3/20 刷题

    1.

    Sample Input
    
        6
        -2 11 -4 13 -5 -2
        10
        -10 1 2 3 4 -5 -23 3 7 -21
        6
        5 -8 3 2 5 0
        1
        10
        3
        -1 -5 -2
        3
        -1 0 -2
        0
    
    Sample Output
    
        20 11 13
        10 1 4
        10 3 5
        10 10 10
        0 -1 -2
        0 0 0
    
    //最基础的DP 数据结构作业
    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 100010;
    int dp[MAXN];	//代表从第一个元素到i的子序列的最大
    int a[MAXN];
    int main () {
    	int K;
    	while(~scanf("%d", &K) && K) {
    		dp[0] = 0;
    		int s1 = -1, e1 = -1, s2 = - 1, e2 = -1;
    		int t = 0;
    		int tttt = -1;
    		memset(dp, 0, sizeof dp);
    		memset(a, 0, sizeof a);
    		for(int i = 1; i <= K; ++i) {
    			scanf("%d", &a[i]);
    			dp[i] = max(a[i], dp[i - 1] + a[i]);
    			if(a[i] < 0)	t++;
    			if(a[i] == dp[i]) {
    				s1 = i;	e1 = i;
    			}
    			else {
    				e1 = i;
    			}
    			if(dp[i] > tttt) {
    				e2 = e1;
    				s2 = s1;
    				tttt = dp[i];
    			}
    		}
    		if(t == K) printf("0 %d %d
    ", a[1], a[K]);
    		else printf("%d %d %d
    ", tttt, a[s2], a[e2]);	
    	}
    }
    

     2.

    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<string, string> P;
    map<string, string> MAP;
    int main () {
    	MAP.insert(P("zero", "0"));
    	MAP.insert(P("one", "1"));
    	MAP.insert(P("two", "2"));
    	MAP.insert(P("three", "3"));
    	MAP.insert(P("four" ,"4"));
    	MAP.insert(P("five" ,"5"));
    	MAP.insert(P("six", "6"));
    	MAP.insert(P("seven", "7"));
    	MAP.insert(P("eight", "8"));
    	MAP.insert(P("nine", "9"));
    	string sb;
    	string t = "";
    	int a, b;
    	while(cin >> sb) {
    		if(sb == "=") {
    			//cout << t << endl;
    			b = std::stoi(t);
    			t = "";
    			if(a == 0 && b == 0) {
    				return 0;
    			}
    			cout << a + b << endl;
    			continue;
    		}
    		if(sb == "+") {
    			//cout << t << endl;
    			a = std::stoi(t);
    			t = "";
    		}
    		t += MAP[sb];
    		// t += (MAP.find(sb)->second);
    		// cout << (MAP.find(sb)->second) << endl;
    	}	
    }
    

     3.

    Sample Input
    
        2
        2
        10 10
        20 20
        3
        1 1
        2 2
        1000 1000
    
    Sample Output
    
        1414.2
        oh!
    
    //最小生成树 prim算法
    #include <bits/stdc++.h>
    using namespace std;
    // const double MAXN = 0x3f3f3f3f;
    #define MAXN 0xFFFFFFF
    const int N = 101;
    typedef pair<double, double> P;
    std::vector<P> v;
    double MAP[N][N];
    bool vis[N];
    int C; //小岛个数
    double getD(P a, P b) {
    	return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
    }
    
    void buildMap() {
    	double l;
    	for(int i = 0; i < C; ++i) {
    		for(int j = i; j < C; ++j) {
    			l = getD(v[i], v[j]);
    			if(l >= 10 && l <= 1000) {
    				//cout << l << endl;
    				MAP[i][j] = MAP[j][i] = ((i == j) ? 0 : l);
    			}
    			else {
    				MAP[i][j] = MAP[j][i] = MAXN;
    			}
    		}
    	}
    	// for(int i = 0 ; i < C; ++i) {
    	// 	for(int j = 0; j < C; ++j) {
    	// 		cout << MAP[i][j] << " ";
    	// 	}
    	// 	cout << endl;
    	// }
    }
    
    void prim() {
    	int temp = C;
    	memset(vis, 0, sizeof vis);
    	vis[0] = 1;
    	double m;
    	double sum = 0;
    	int sb;
    	while(--temp) {
    		m = MAXN;
    		for(int i = 1; i < C; ++i) {
    			if(!vis[i] && MAP[0][i] < m){
    				//
    				m = MAP[0][i];
    				sb = i;
    				//cout << m << "???"<<endl;
    			} 
    		}
    		if(m == MAXN)	break;
    		vis[sb] = 1;
    		// cout << m << endl;
    		sum += m;
    		for(int i = 1; i < C; ++i) {
    			if(!vis[i] && MAP[sb][i] < MAP[0][i])
    				MAP[0][i] = MAP[sb][i];
    		}
    	}
    	//cout << temp << endl;
    	if(temp == 0) {
    		printf("%.1f
    ", sum * 100.0);
    	}
    	else {
    		cout << "oh!" << endl;
    	}
    }
    int main () {
    	int T;
    	cin >> T;
    	while(T--) {
    		v.clear();
    		cin >> C;
    		for(int i = 0; i < C; ++i) {
    			double x, y;
    			cin >> x >> y;
    			v.push_back(P(x, y));
    		}
    		buildMap();
    		prim();	
    	}
    }
    

     4.

    Sample Input
    
        3
        1 2 1 0
        1 3 2 0
        2 3 4 0
        3
        1 2 1 0
        1 3 2 0
        2 3 4 1
        3
        1 2 1 0
        1 3 2 1
        2 3 4 1
        0
    
    Sample Output
    
        3
        1
        0
    
    //也是最小生成树 注意已经修好的路不用花钱
    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 0x3fffffff;
    const int N = 110;
    bool vis[N];
    int MAP[N][N];
    int n;
    int low[110];
    void prim() {
    	memset(vis, 0, sizeof vis);
    	int ans = 0;
    	int k = 0;
    	vis[k] = 1;
    	// for(int i = 0; i < n; ++i) {
    	// 	if(!vis[i]) {
    	// 		low[i] = MAP[k][i];
    	// 	}
    	// }
    	int m;
    	for(int i = 1; i < n; ++i){
    		m = MAXN;
    		for(int i = 0; i < n; ++i){
    			if(!vis[i] && MAP[0][i] < m) {
    				k = i;
    				m = MAP[0][i];
    			}
    		}
    		ans += m;
    		vis[k] = 1;
    		for(int i = 0; i <= n + 1; ++i) {
    			if(!vis[i] && MAP[i][k] < MAP[0][i]) {
    				MAP[0][i] = MAP[i][k];
    			}
    		}	
    	}
    	cout << ans << endl;
    }
    int main () 
    {
    	ios::sync_with_stdio(false);
    	while(cin >> n) {
    		if(!n)	break;
    		int x, y, s, b;
    		int t = (n - 1) * n / 2;
    		while(t--){
    			cin >> x >> y >> s >> b;
    			if(b) {
    				MAP[x-1][y-1] = MAP[y-1][x-1] = 0;
    			}
    			else {
    				MAP[x-1][y-1] = MAP[y-1][x-1] = s;
    			}
    		}
    		prim();
    	}
    }
    

     5.leetcode上的一道憨批题

    //用O(n方)时间复杂度写出来的我着实憨批
    class Solution {
    public:
    string pushDominoes(string dominoes) {
    
    	for(int i = 0; i < dominoes.size(); ++i) {
    		if(dominoes[i] != '.' && dominoes[i] != 'M' && dominoes[i] != 'S') {
                continue;
            }
            int R, L;
    		R = 0x3f3f3f3f; L = 0x3f3f3f3f;
            int s1, s2;
            s1 = i; s2 = i;
    		char t1, t2;
    		for(int j = i + 1; j < dominoes.size(); ++j) {
    			if(dominoes[j] != '.' && dominoes[j] != 'M' && dominoes[j] != 'S') {
                    if(dominoes[j] == 'L'){
                       	t1 = dominoes[j];
    				    R = j - s1;
                    }
    				break;
    			}
    		}
            if(i == 5)
            cout << 66 << endl;
    		for(int j = i - 1; j >= 0; --j) {
                if(j == 3 && i == 5) cout << dominoes[i] << endl;
    			if(dominoes[j] != '.' && dominoes[j] != 'M' && dominoes[j] != 'S') {
    				if(dominoes[j] == 'R'){
                        if(j == 3 && i == 5) cout << "sb" << endl;
                        t2 = dominoes[j];
                        L = s2 - j;
                    }	
    				break;
    			}
    		}
            if(i == 5) {
                cout << R << " " << L << endl;
                cout << dominoes[i] << endl;
            }
    		if(R < L) {
    			dominoes[i] = t1 + 1;
    		}
    		else if(R > L) {
    			dominoes[i] = t2 + 1;
    		}
    		else {
    			dominoes[i] = '.';
    		}
    	}
        for(int i = 0; i < dominoes.size(); ++i) {
            if(dominoes[i] != 'L' && dominoes[i] != 'R' && dominoes[i] != '.') {
                 dominoes[i]--;
            }
        }
     	return dominoes;
    }
    };
    

     

    //这是真的憨批题了
    class Solution {
    public:
        bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
            if(rec1[2] > rec2[0] && rec1[0] < rec2[2] && rec1[3] > rec2[1] && rec1[1] < rec2[3])
                return 1;
            return 0;    
        }
    };
    

     

    //挺好的一道dp题
    class Solution { public: int min(int a, int b) { return a > b ? b : a; } int numSquares(int n) { int dp[100000] = {0}; for(int i = 1; i <= n; ++i) { dp[i] = i; for(int j = 1; i - j * j >= 0; j++) { dp[i] = min(dp[i], dp[i - j * j] + 1); } } return dp[n]; } };

    明天上午学习课程!!!!!!学习课程!!!!!!学习课程!!!!!!学习课程!!!!!!

    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
    LeetCode 216. 组合总和 III(Combination Sum III)
    LeetCode 179. 最大数(Largest Number)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)
    指针变量、普通变量、内存和地址的全面对比
    MiZ702学习笔记8——让MiZ702变身PC的方法
    你可能不知道的,定义,声明,初始化
    原创zynq文章整理(MiZ702教程+例程)
  • 原文地址:https://www.cnblogs.com/lightac/p/12521036.html
Copyright © 2011-2022 走看看