이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
//dp[i][j] = es posible satisfacer a los clientes i con j monedas
int main(){
int n, m;
cin >> n >> m;
int people[n];
int coins[m];
unordered_map<int, vi> coins_to_persons;
unordered_map<int, vi> persons_to_combs;
for (int i = 0; i < n; ++i) {
cin >> people[i];
if(coins_to_persons.find(people[i]) == coins_to_persons.end()) coins_to_persons[people[i]] = {i};
else coins_to_persons[people[i]].push_back(i);
}
//cout << " <" << endl;
for(int i = 0; i < m; ++i){
cin >> coins[i];
//cout << i << " " << coins[i] << endl;
}
//cout << " << " << endl;
int dp[n][1<<m];
memset(dp, 0, sizeof(dp));
//cout << " a " << endl;
for(int i = 0; i < (1 << m); ++i){
//cout << "aa" << endl;
//cout << i << endl;
int ans = 0;
for(int j = 0; j < m; ++j){
if(i&(1 << j)) ans+= coins[j];
}
//cout << ans << endl;
for(int pers: coins_to_persons[ans]){
if(persons_to_combs.find(pers) == persons_to_combs.end()) persons_to_combs[pers] = {i};
else persons_to_combs[pers].push_back(i);
//cout << ans <<" " <<i << " " << pers << endl;
}
}
for(int i: persons_to_combs[0]){
dp[0][i] = 1;
if(n == 1) {
cout << "YES" << endl;
return 0;
}
}
bool ans = false;
//cout << "a" << endl;
for(int who = 1; who < n; ++who){
for(int mask = 0; mask < (1 << m); ++mask){
for(int comb : persons_to_combs[who]){
if((mask & comb) == comb) dp[who][mask] = max(dp[who][mask], dp[who-1][mask^comb]);
//cout << who-1 << " " << comb << " "<< mask << " " << dp[who][mask]<< endl;
}
//cout << who << " " << mask << " " << dp[who+1][mask] << endl;
if((who == n-1) and dp[who][mask]) {
ans = true;
cout << "YES" << endl;
return 0;
//cout << who << " " << mask << endl;
}
}
}
if(ans) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |