zoukankan      html  css  js  c++  java
  • Daliy Algorithm (贪心,gcd)-- day 58

    Nothing to fear

    those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


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

    2020.4.16


    lg-田忌赛马

    贪心
    贪心策略的思考还不是很完善还需要多练练

    1. 如果田忌目前的最快马快于齐王目前的最快马,则两者比
    2. 如果田忌的最快马慢于齐王的最快马,则用田忌的最慢马与齐王的最快马比((减少损失))
    3. 如果田忌的最快马和齐王的最快马相等,分以下两种情况:
      3.1 若田忌的最慢马快与齐王的最慢马,两者比(能赢就赢呗)
      3.2 其他,用田忌的最慢马与齐王的最快马比(贡献最大)
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    
    using namespace std;
    const int N = 2005; 
    int a[N] ,b[N] , n;
    int main()
    {
    	SIS;
    	cin >> n;
    	for(int i = 1;i <= n ;i ++)cin >> a[i];
    	for(int i = 1;i <= n ;i ++)cin >> b[i];
    	sort(a + 1, a + 1 + n);
    	sort(b + 1, b + 1 + n);
    	int ai = 1, bi = 1,bj = n , aj = n;
    	int ans = 0;
    	for(int i = 1;i <= n ;i ++){
    		if(a[aj] > b[bj]){
    			aj--;bj--;ans++;
    		}
    		else if(a[aj] < b[bj]){ 
    			ai++;bj--;ans--;
    		}
    		else if(a[ai] > b[bi]){
    			ai++;bi++;ans++;
    		}
    		else{
    			if(a[ai] < b[bj])ans--;
    			ai++;bj--;
    		}
    	}
    	cout << ans * 200 << endl;
    	return 0;
    }
    
    

    等差数列

    首先项数可以由最大值和最小值和公差得到
    (n = (a_{n} - a_{1}) / d)
    关键在于怎么求解d
    每一项与第一项的差一定是d的倍数
    当d != 0 时, (a末 - a初) / d + 1 ---- 让公差d最大即可
    当d == 0 时,答案为 n

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    #include <climits>
    
    using namespace std;
    const int N = 100005;
    
    int a[N] , n;
    
    int gcd(int a,int b)
    {
    	return b ? gcd(b , a % b) : a;
    }
    int main()
    {
    	cin >> n;
    	for(int i = 1;i <= n ;i ++)
    	{
    		scanf("%d",&a[i]);
    	}
    	sort(a + 1, a + 1 + n);
    	int t = 0;
    	for(int i = 1;i <= n ;i ++)
    	{
    		t = gcd(t , a[i] - a[1]);
    	}
    	cout << t << endl;
    	if(!t)
    	{
    		cout << n << endl;
    		return 0;
    	}else{
    		cout << (a[n] - a[1]) / t + 1;
    	}
    	return 0;
    }
    

    第十届H签到题:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <vector>
    
    using namespace std;
    int t;
    int gcd(int a,int b)
    {
    	return b ? gcd(b , a % b) : a;
    }
    // 找到一个数和这两个数的最大公约数达到最大
    void work()
    {
    	int x , y;
    	cin >> x >> y;
    	cout << gcd(x , y) + x + y << endl;
    }
    int main()
    {
    	cin >> t;
    
    	while(t--)
    	{
    		work();
    	}
    	return 0;
    }
    
  • 相关阅读:
    用户访问集群架构的流程
    HTTP请求方法 HTTP的响应方法
    数据报文
    什么是HTTP? 什么是超文本? 什么是URL?
    HTTP协议原理
    C语言之数据类型③——字符与字符串
    C语言之数据类型②——浮点类型
    C语言之数据类型①——整数类型
    uniapp自定义小程序左上角的图标并且添加自定义事件
    在uniapp中使用iconfont
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12716131.html
Copyright © 2011-2022 走看看