当前位置:网站首页>ACM winter vacation training 6
ACM winter vacation training 6
2022-07-28 10:28:00 【MJ's Manon space】
Greedy Algorithm
Greedy Algorithm ( Also known as greedy algorithm ) Refer to , In solving the problem , Always make what seems to be the best choice at the moment . Greedy algorithm can not get the overall optimal solution for all problems , The key is the choice of greedy strategy , in other words , Don't take into account the overall optimum , The algorithm obtains the local optimal solution in a sense .
Algorithm ideas
Greedy algorithm generally follows the following steps :
① Build a mathematical model to describe the problem .
② Divide the problem into several sub problems .
③ Each subproblem is solved , Get the local optimal solution of the subproblem .
④ Synthesize the local optimal solution of the subproblem into a solution of the original solution .
Greedy algorithm is a simpler solution to some problems 、 Faster design technology . Greedy algorithm is characterized by step by step , It is often based on the current situation and makes the optimal choice according to an optimization measure , Regardless of the possible overall situation , It saves a lot of time that must be spent trying everything possible to find the optimal solution . The greedy algorithm adopts top-down , Make successive greedy choices in an iterative way , Every greedy choice , The problem is reduced to a smaller subproblem , Through every step of greedy choice , An optimal solution of the problem can be obtained . Although it is necessary to ensure that the local optimal solution can be obtained at each step , However, the resulting global solution is sometimes not necessarily optimal , So greedy algorithms don't go back .
Conditions of use
The problems solved by greedy method should have the following 2 Features .
1、 Greedy choice nature
The global optimal solution of a problem can be achieved by selecting a series of local optimal solutions , And each choice can depend on the choice made before , But it doesn't depend on the choices to be made later . This is the nature of greedy choice . For a specific problem , To determine whether it has the nature of greedy choice , It must be proved that the greedy choice made at each step leads to the global optimal solution of the problem .
2、 Optimal substructural properties
When the optimal solution of a problem contains the optimal solution of its subproblem , We call this problem an optimal substructure . The property of optimal substructure is the key to solve the problem by greedy method . in application , As for what question has what kind of greedy choice nature is uncertain , Need specific analysis .
Title Example
“ This summer vacation is not good AC?”
“ Yes .”
“ So what are you doing ?”
“ Watch the world cup , Idiot !”
“@#$%^&*%…”
Such is the case , Here comes the world cup , Fans' Festival is coming too , It's estimated that a lot of ACMer And I'll leave the computer alone , I'm on TV .
As a fan , I want to see as many complete games as possible , Of course , As a good youth in the new era , You're going to watch some other shows , Like the news network ( Never forget to care about national affairs )、 very 6+7、 Super girl , And Wang Xiaoya's 《 Happy Dictionary 》 wait , Suppose you already know the schedule of all your favorite TV shows , Will you make a reasonable arrangement ?( The goal is to see as many full shows as possible )
Input
Input data contains multiple test instances , The first line of each test instance has only one integer n(n<=100), The total number of programs you like to watch , And then there was n Row data , Each row contains two data Ti_s,Ti_e (1<=i<=n), Separate indication control i The start and end time of a program , To simplify the problem , Each time is represented by a positive integer .n=0 End of input , Don't deal with it .
Output
For each test case , Output the number of TV programs that can be seen completely , The output of each test instance takes up one line .
Sample Input
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
Sample Output
5
Code example
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n!=0)// Multi group input
{
int a[110],b[110],i,j,k,t,num=1;
for(i=0;i<n;i++)
scanf("%d %d",&a[i],&b[i]);
for(i=0;i<n-1;i++)// Sort by end time
{
k=i;
for(j=i+1;j<n;j++)
{
if(b[k]>b[j])// Find the program that ends earliest
k=j;
}
t=a[i];// Switch the earliest programs to the front
a[i]=a[k];
a[k]=t;
t=b[i];
b[i]=b[k];
b[k]=t;
}
j=0;
for(i=1;i<n;i++)// If the program time does not conflict num++
{
if(b[j]<=a[i])
{
num++;
j=i;
}
}
printf("%d\n",num);// Output
}
return 0;
}
边栏推荐
- Double pointer technique
- 问题总结档案
- a different object with the same identifier value was already associated with the session
- 7、二分法——寻找一组重复或者有序但是旋转的数组
- ACM寒假集训#7
- django-celery-redis异步发邮件
- 3. Print the linked list in reverse order with the array
- Why does the cluster need root permission
- 2. Output one of the repeated numbers in the array
- 配置树莓派,过程和遇到问题
猜你喜欢

SQL Server 2016 learning records - connection query

用两个栈实现一个队列【C语言】

6. Double pointer -- the sum of the two numbers of the incremental array is equal to the target number

SQL Server 2016 learning records - data update

SQL Server 2016 learning record - Data Definition

PHP generates QR code (learning)

Aqua Data Studio 18.5.0导出insert语句

IDEA创建我的第一个项目

SQL Server 2016学习记录 --- 连接查询

11、链表反转
随机推荐
吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
Pl/sql server syntax explanation
多线程与高并发(三)—— 源码解析 AQS 原理
10 minute quick start EVs [play Huawei cloud]
UEditor V1.4.3控制文件压缩
阿里云镜像地址
12、双指针——合并两个有序链表
Get to know SuperMap idesktop for the first time
2. Output one of the repeated numbers in the array
a different object with the same identifier value was already associated with the session
Codeforces Round #614 (Div. 2) A. ConneR and the A.R.C. Markland-N
管道、管程、管态的区别
gcc: error trying to exec 'as': execvp: No such file or directory
Leetcode076 -- the kth largest number in the array
SQL Server 2016 learning records - connection query
第一篇:UniAPP的小程序跨端开发-----创建uniapp项目
SQL Server 2016 learning record - Data Definition
ogg里用多个filter语法应该怎么写?
7、二分法——寻找一组重复或者有序但是旋转的数组
字符串匹配