码迷,mamicode.com
首页 > 其他好文 > 详细

题解 P1469 【找筷子】

时间:2019-02-15 21:17:24      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:print   ttl   max   std   需要   namespace   name   pac   作者   

这题真是水
咳咳。。
基本思路:桶排
但是可以剪枝。


剪枝方法:

好几种,可以用set(集合),可以用stack(栈)
也可以像我一样的蒟蒻最大最小值......
但是作者的毒瘤数据应该不会放过我们的...


AC code奉上

#include <iostream>
#include <cstdio>
using namespace std;
int bfs(int bottle[],int max_,int min_)
{
    for (int i=min_;i<=max_;i++)
    {if (bottle[i]%2==1)return i;}
}
//基本思路:桶排 
//剪枝优化方式:最大最小值 
int main()
{
    int n,max_=0,min_=10000010;
    static int bottle[10000001];
    //方便的是,静态数组默认为0 
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int temp;scanf("%d",&temp);
        bottle[temp]++;
        max_=max_>temp?max_:temp;
        min_=min_<temp?min_:temp;
    }
    int num=bfs(bottle,max_,min_);
    printf("%d",num);
    return 0;
}

集合的思想就是去重
查找的时候只需要一个个抽元素出来找就行。
可以大大减少查找次数。
话说快排真的有必要吗

题解 P1469 【找筷子】

标签:print   ttl   max   std   需要   namespace   name   pac   作者   

原文地址:https://www.cnblogs.com/jelly123/p/10385896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!