感觉很多贪心的题目只要想到怎么贪心就很快能解决,但是没有想到的话代码量就会很大,而且很容易出错,所有贪心还是要多做题目,掌握各种贪心的题目

题目链接:https://vjudge.net/contest/231313#problem/D

A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.

Input

The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.

Output

The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.

Sample Input

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0 

Sample Output

2
1 

题目大意:有1*1,2*2···6*6尺寸的箱子,每种物品的高度确定,只用6*6的箱子,输入每种尺寸的物品有多少个,问你最少要用多少箱子,当六个0时推出循环个人思路:6*6,5*5,4*4的物品每一个都需要一个箱子来放,所以先考虑,4个3*3物品占满一个箱子,考虑要多少个箱子,然后考虑已经用了的箱子能放多少个2*2的,不够的话再用箱子最后考虑总共用了多少个箱子,把方块数求出来,减去用掉的方块数,就算当前的箱子能放的1*1的物品数量,不够再补箱子
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
#define INF 0x3f3f3f
int main()
{ll a[10];ll num[4]={0,5,3,1};while(1){ll ans=0;ll tmp=0;for(int i=1;i<=6;i++){scanf("%I64d",&a[i]);tmp+=a[i];}if(tmp==0)return 0;ans+=a[6]+a[5]+a[4]+(a[3]+3)/4;//a6,a5,a4每个物品需要一个箱子,4个a3需要一个箱子ll a2=a[4]*5+num[a[3]%4];//a6,a5,a4,a3有多少个空位子可以放2*2的物品if(a[2]>a2){ans+=(a[2]-a2+8)/9;}int a1=ans*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;//总共用的箱子还剩多少个能放a1if(a[1]>a1){ans+=(a[1]-a1+35)/36;}printf("%I64d\n",ans);}return 0;
}

  

题目链接:https://vjudge.net/contest/231317#status/1751151850/D/0/

Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.

Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.

Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.

The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.

Output

Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.

Examples

Input
5rbbrr

Output
1

Input
5bbbbb

Output
2

Input
3rbr

Output
0

Note

In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.

In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.

In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.

题目大意:输入一个长度为n的字符串,你可以交换两个字符或者改变任意一个字符,问你最少操作次数,使得br交替出现

个人思路:其实就是有四种情况:

1、当第一个字符为b时,你可以考虑要不要将它改变为r。  这里就是两种情况,改变或者不改变

2、当第一个字符为r时,你可以考虑要不要讲它改变为b。 这里也是两种情况,改变或者不改变

为什么是这样呢,因为任意一个字符串,只有第一个确定了,后面的也都确定了

看代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
#define INF 0x3f3f3f
char a[100050];
char b[100050];
int n;
int solve(int p,int q,int sum1,int sum2)
{while(q!=n){if(b[q]==b[p]&&b[p]=='r'){b[q]='b';sum1++;p++;q++;}else if(b[q]==b[p]&&b[p]=='b'){b[q]='r';sum2++;p++;q++;}else{p++;q++;}}return max(sum1,sum2);//注意这里是什么意思,sum1代表r需要改变的次数,sum2代表b需要改变的次数,所以可以                              //交换的次数就是min(sum1,sum2),而剩下的就必须是改变的,大的减去小的加上min(sum1,sum2)就等价于max(sum1,sum2)
}
int main()
{int ans1,ans2;//sum1是b的转移次数,sum2是r的转移次数cin>>n;cin>>a;strcpy(b,a);//cout<<b;if(b[0]=='r'){ans1=solve(0,1,0,0);strcpy(b,a);b[0]='b';ans2=solve(0,1,1,0);}else if(b[0]=='b'){ans1=solve(0,1,0,0);strcpy(b,a);b[0]='r';ans2=solve(0,1,0,1);}// cout<<sum1<<sum2<<sum3<<sum4<<endl;cout<<min(ans1,ans2);return 0;
}

题目链接:https://vjudge.net/contest/231316#problem/D

Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.

In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.

What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

Input

The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

Output

Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

Examples

Input
6 3

Output
4

Input
8 5

Output
3

Input
22 4

Output
6

Note

In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .

In the second sample test, Memory can do .

In the third sample test, Memory can do:

.

题目大意:输入x,y,x代表刚开始的等边三角形的边长,y代表最终的等边三角形的边长,问你从x变为y,最少要变多少次,注意,变的过程中也要是三角形

个人思路:刚开始在找规律,应该说找到一个错误的规律,所以wa了,然后没什么想法了,百度了一下,看到个逆序来求,然后瞬间把它秒了,就是逆序来做

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const ll maxa=1e10;
#define INF 0x3f3f3f3f3f3f
int a[5];
int x,y,ans=0;
void solve()
{sort(a,a+3);int t=a[1]+a[2]-1;if(t<=x){a[0]=t;ans++;}else{a[0]=x;ans++;}
}
int main()
{cin>>x>>y;for(int i=0;i<3;i++)a[i]=y;while(a[0]!=x||a[1]!=x||a[2]!=x){solve();}cout<<ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9304259.html

贪心算法(各种贪心题目)相关推荐

  1. java 贪心算法思路,贪心算法之——黑白点的匹配(两种实现方法),贪心算法...

    贪心算法之--黑白点的匹配(两种实现方法),贪心算法 一.题目 设平面上分布着n个白点和n个黑点,每个点用一对坐标(x, y)表示.一个黑点b=(xb,yb)支配一个白点w=(xw, yw)当且仅当x ...

  2. 贪心算法之贪心的加勒比海盗

    14天阅读挑战赛 努力是为了不平庸~ 算法学习有些时候是枯燥的,这一次,让我们先人一步,趣学算法!欢迎记录下你的那些努力时刻(算法学习知识点/算法题解/遇到的算法bug/等等),在分享的同时加深对于算 ...

  3. java调度问题的贪心算法_贪心算法——换酒问题

    知识回顾 贪心算法 (greedy algorithm),又称贪婪算法. 是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是最好或最优的算法. 贪心算法在 有最优子 ...

  4. 田忌赛马贪心算法_贪心算法之田忌赛马

    你一定听过田忌赛马的故事吧?       如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉20 ...

  5. 贪心算法之贪心的c小加问题

    NYOJ-236 心急的C小加 心急的C小加 时间限制: 1000 ms  |           内存限制: 65535 KB 难度: 4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一 ...

  6. 田忌赛马贪心算法_贪心算法--田忌赛马问题

    题目描述: 你一定听过田忌赛马的故事吧? 如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉20 ...

  7. java编程贪心算法背包问题,贪心算法----部分背包问题(java实现)

    部分背包问题 给定 n 种物品和一个背包.物品 i 的重量是 Wi,其价值为 Vi,背包的容量为 C.在选择物品 i 装入背包时,可以选择物品 i 的一部分,1<= i <=n.问应如何选 ...

  8. 田忌赛马贪心算法_贪心算法解决田忌赛马

    田忌赛马--对策问题 教学目标: 1.通过简单的事例,使学生初步体会对策论在解决... 梁锦美有无课件 上课时间 有 12 月 15 日 1 田忌赛马中的数学问题 课时... 同学们,你听过" ...

  9. php自动排课贪心算法,关于贪心法的排课算法.pdf

    25 3 V o . 25 No. 3 2005 5 J ourna of Y unnan Norm a Universit y M ay 2005 * 梁 立, 陈玉华, 徐 敏 ( , 65009 ...

最新文章

  1. python读取yuv
  2. android getprop 分辨率,Android getprop 读取的属性哪里来的?
  3. clientHeight,offsetHeight,scrollHeight迷一样的三个值
  4. vue --- 全局注册子组件,并导入全局的子组件
  5. java中方法的参数传递机制
  6. 一张纸厚度大约是什么_折叠103次,纸厚度就能超过宇宙直径?科学家解释让人如坠冰窖!...
  7. USACO 2.2 集合(DP)
  8. MySQL高级进阶(三)、InnoDB存储引擎详解
  9. 微信小游戏 资源下载解压
  10. 图片清晰度差怎么修复成高清图片
  11. 使用POI在Excel单元格插入符号(Symbol)
  12. 什么是软件外包公司?要不要去外包公司?
  13. vue3 权限菜单( 树形菜单)无限循环
  14. Flask-定时任务
  15. opencv 修改图像像素
  16. Python爬虫获取异步加载站点pexels并下载图片(Python爬虫实战3)
  17. 西门子PLC控制器与ModbusTCP工业RFID读写器CK-FR08-E00的通信操作实例
  18. 记录:谷歌地图google map api实现基本测距功能
  19. iOS小技能: 集成社会化分享(代理商展业二维码)
  20. Mac安装 anaconda及其基本命令

热门文章

  1. js创建对象的几种常用方式小结(推荐)
  2. 多线程学习笔记4 互斥体
  3. [必看]首先要求做到的事情![sumtec]
  4. 中国产业数字化发展报告:数智创新,智驱未来
  5. linux跑程序占用缓存过多释放内存echo 3 > /proc/sys/vm/drop_caches
  6. 对于Neural ODE的小研究
  7. python深度学习第四讲——python神经网络参数更新
  8. 概率图模型笔记(二) 隐马尔科夫模型(Hidden Markov Model)
  9. hadoop集群_使用docker部署hadoop集群
  10. java里不支持post请求_java – Spring Boot – 不支持请求方法’POST’