E - Kth Takoyaki Set
DP 思维好题
Time Limit: 3 sec / Memory Limit: 1024 MB
Score : 500500 points
Problem Statement
In AtCoder Kingdom, N kinds of takoyakis (ball-shaped Japanese food) are sold. A takoyaki of the i-th kind is sold for Ai yen.
Takahashi will buy at least one takoyaki in total. He is allowed to buy multiple takoyakis of the same kind.
Find the K-th lowest price that Takahashi may pay. Here, if there are multiple sets of takoyakis that cost the same price, the price is counted only once.
Constraints
- 1≤N≤10
- 1≤K≤2×105
- 1≤Ai≤109
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A1 A2 …… AN
Output
Print the answer as an integer.
题意:给定我们n 个数 每个数字可以取无限次 问你 随意取任意个数的情况下的第k小数字是什么?
分析:每次都是在上一次已经取出的最小的情况下再在我们1 - n 中进行取的时候才是最优的
反证很容易证明这个,假设我们当前再取一个是不在上一次取的最优的情况下取一个得到 那我们上次就不是最优的 ,得证
然后用集合维护一下就行了 这个题k*n*logn 时限给的足够不会卡常数
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<ll>st;
int n,k;
int main()
{
cin>>n>>k;
vector<ll>a(n+1);
for(int i=1;i<=n;i++)cin>>a[i],st.insert(a[i]);
k--;//去除k-1 个 便是要求的第k大~
while(k--)
{
ll t=*st.begin();
for(int i=1;i<=n;i++)
st.insert(t+a[i]);
st.erase(t);
}
cout<<*st.begin();
}