总结--(新手易懂)

备战ACM省赛

对于这两天的考核感觉很不好,题目也看不懂,知识点也掌握的不是很牢固,所以现在总结一下。

二分

二分不止可以查找数,也可以查找邻近的数。

int binary_search(int data[],int n,int x)
{
if(n <=0|| x < data[0]|| x > data[n-1]){
return-1;
}
int left =0, right = n-1;
int middle;
while(left <= right){
middle =(left + right)/2;
if(x == data[middle]){
return middle;
}elseif(x > data[middle]){
left = middle +1;
}else{
right = middle -1;
}
}
return-1;
}

贪心

贪心

现在学的贪心只是简单的,可以解决背包问题,会场分布问题等经典问题。举一个例子:
例题:
Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A…B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

Help FJ by determining:

The minimum number of stalls required in the barn so that each cow can have her private milking period
An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.

	Input

Line 1: A single integer, N

Lines 2…N+1: Line i+1 describes cow i’s milking interval with two space-separated integers.

	Output

Line 1: The minimum number of stalls the barn must have.

Lines 2…N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

	Sample Input5

1 10
2 4
3 6
5 8
4 7
Sample Output4
1
2
3
2
4
Hint
Explanation of the sample:

Here’s a graphical schedule for this output:

Time 1 2 3 4 5 6 7 8 9 10
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

Stall 2 … c2>>>>>> c4>>>>>>>>> … …

Stall 3 … … c3>>>>>>>>> … … … …

Stall 4 … … … c5>>>>>>>>> … … …Other outputs using the same number of stalls are possible.
题目意思是挤牛奶,给你几组数据分别为每头牛的挤奶时间,让你找用最少的挤奶工人并且输出每头牛的挤奶工的编号。
代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxn=60000;
ll n,use[maxn];

struct node
{
    ll b;
    ll c;
    ll d\\用来记录开始时的顺序。;
    bool operator <(const node &a)const\\在队列中的排序,按c来排,当c一样再排b。
    {
        if(c等于a.c)
            return b>a.b;
        return c>a.c;
    }
} a[maxn];
priority_queue<struct node> q;\\优先队列,还说明队列中是一个结构体。
ll cmp(struct node x,struct node y)
{
    return x.b<y.b;
    if(x.b==y.b)
        return x.c<y.c;
}
int main()
{
    ll i,j,k=1;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lld %lld",&a[i].b,&a[i].c);
        a[i].d=i;
    }
    sort(a,a+n,cmp)\\sort函数,按b的大小排列;
    use[a[0].d]=k;
    while(!q.empty())
        q.pop();
    q.push(a[0]);
    for(i=1;i<n;i++)
    {
        if(!q.empty()&&q.top().c<a[i].b)
        {
            use[a[i].d]=use[q.top().d];
            q.pop();
        }
        else
        {
            k++;
            use[a[i].d]=k;
        }
        q.push(a[i]);
    }
    printf("%lld\n",k);
    for(i=0;i<n;i++)
        printf("%lld\n",use[i]);
     while(!q.empty())
        q.pop();
        return 0;
}

队列

[介绍队列基本功能](https://blog.csdn.net/lee371042/article/details/81135007)

搜索

搜索有广搜和深搜,就是一次搜到底和一层一层的搜。

dp

动态规划问题满足三大重要性质

最优子结构性质:如果问题的最优解所包含的子问题的解也是最优的,我们就称该问题具有最优子结构性质(即满足最优化原理)。最优子结构性质为动态规划算法解决问题提供了重要线索。
子问题重叠性质:子问题重叠性质是指在用递归算法自顶向下对问题进行求解时,每次产生的子问题并不总是新问题,有些子问题会被重复计算多次。动态规划算法正是利用了这种子问题的重叠性质,对每一个子问题只计算一次,然后将其计算结果保存在一个表格中,当再次需要计算已经计算过的子问题时,只是在表格中简单地查看一下结果,从而获得较高的效率。
无后效性:将各阶段按照一定的次序排列好之后,对于某个给定的阶段状态,它以前各阶段的状态无法直接影响它未来的决策,而只能通过当前的这个状态。换句话说,每个状态都是过去历史的一个完整总结。这就是无后向性,又称为无后效性。
动态规划求他的最优解要先求他的子序列的最有,从小到大都为最优自然就找到了最优解,现在遇到的问题大都要用二维数组,记录下前面的最优解。
dp链接[DP算法总结1](https://blog.csdn.net/qq_1932568757/article/details/82725132
2;[DP思路解算法题](https://blog.csdn.net/GG_SiMiDa/article/details/62434913

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值