码迷,mamicode.com
首页 > Web开发 > 详细

[题解] [JSOI2010] 找零钱的洁癖

时间:2020-02-04 11:03:48      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:res   bfs   turn   ref   pen   就是   lin   har   iostream   

题面

题解

说实话, 其实我也不知道这题正解是啥
你看着数据范围不像爆搜题
但是爆搜他就是可以过, 我也不知道为啥
奇葩

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;

int X, a[1005], cnt, q[1000005], num[1000005]; 
map<int, bool> mp; 

template < typename T >
inline T read()
{
    T x = 0, w = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * w; 
}

int greedy()
{
    int res = 0, sum = X;
    for(int i = cnt; i; i--)
    {
        res += sum / a[i];
        sum -= sum / a[i] * a[i]; 
    }
    return sum ? 0x3f3f3f3f : res; 
}

int bfs()
{
    int l = 1, r = 0;
    q[++r] = 0, num[r] = 0; 
    while(l <= r && r <= 1000000)
    {
        for(int i = 1; i <= cnt; i++)
        {
            q[++r] = q[l] + 1, num[r] = num[l] > X ? num[l] - a[i] : num[l] + a[i]; 
            if(num[r] == X) return q[r]; 
            if(mp[num[r]] == 1) { r--; continue; }
            mp[num[r]] = 1; 
        }
        l++; 
    }
    return 0x3f3f3f3f; 
}

int main()
{
    X = read <int> ();
    while(scanf("%d", &a[++cnt]) != EOF);
    cnt--; sort(a + 1, a + cnt + 1); 
    if(!X) puts("0");
    else printf("%d\n", min(greedy(), bfs())); 
    return 0; 
}

[题解] [JSOI2010] 找零钱的洁癖

标签:res   bfs   turn   ref   pen   就是   lin   har   iostream   

原文地址:https://www.cnblogs.com/ztlztl/p/12258451.html

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