ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Daliy Algorithm (贪心,gcd)-- day 58

2020-04-16 22:01:32  阅读:249  来源: 互联网

标签:Daliy 58 Algorithm int 最快 bj ++ ai include


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;
}

标签:Daliy,58,Algorithm,int,最快,bj,++,ai,include
来源: https://www.cnblogs.com/wlw-x/p/12716131.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有