国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

1068. Find More Coins (30)

2019-11-08 02:58:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

可能排序太費(fèi)時(shí)間,用map話對(duì)于重復(fù)的直接次數(shù)+1即可,不需要排序

最后個(gè)測(cè)試點(diǎn)超時(shí)的代碼

#include<iostream>#include<vector>#include<algorithm>#PRagma warning(disable:4996)using namespace std;int N, M;vector<int> all;int add = 0;vector<int> path;void traver(int index){ if (add == M) { int flag = 0; for (auto x : path) if (flag == 0) { printf("%d", x);flag = 1; } else printf(" %d", x); printf("/n"); exit(0); } if (add + all[index + 1] > M) return; for (int t = index + 1;t < N;t++) { if (add + all[t] > M) break; path.push_back(all[t]); add += all[t]; traver(t); path.pop_back(); add -= all[t]; }}int main(){ scanf("%d %d", &N, &M); all.resize(N); for (int t = 0;t < N;t++) scanf("%d", &all[t]); sort(all.begin(), all.end()); for (int t = 0;t < N;t++) { if (all[t] > M) break; path.push_back(all[t]); add = all[t]; traver(t); path.pop_back(); } printf("No Solution/n");}

通過(guò)的代碼

#include<iostream>#include<vector>#include<map>#include<algorithm>#pragma warning(disable:4996)using namespace std;int N, M;map<int,int> all;int add = 0;vector<int> path;bool findc(int M){ auto it = all.begin(); while (it!=all.end() && it->first <= M) { if (it->second) { it->second--; if ((*it).first == M) { path.push_back(it->first); return true; } else if (findc(M - it->first)) { path.push_back(it->first); return true; } else { it->second++; } } it++;; } return false;}int main(){ scanf("%d %d", &N, &M); for (int t = 0;t < N;t++) { int temp; scanf("%d", &temp); if (temp <= M) all[temp] += 1; } if(!findc(M)) printf("No Solution/n"); else { int first=1; for (auto it = path.rbegin();it != path.rend();it++) if (first) { printf("%d", *it); first--; } else printf(" %d", *it); cout << endl; }}
上一篇:加載外部資源

下一篇:2017-02-18

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 商水县| 凯里市| 定襄县| 武义县| 灵台县| 井研县| 盐城市| 沛县| 突泉县| 额尔古纳市| 历史| 当阳市| 磐石市| 无极县| 修武县| 永清县| 南城县| 巧家县| 广饶县| 新丰县| 日喀则市| 舞阳县| 铜鼓县| 垦利县| 牙克石市| 嵊州市| 固阳县| 伊通| 潞西市| 当雄县| 邳州市| 宾川县| 太康县| 苗栗县| 云和县| 新干县| 玉山县| 上杭县| 大石桥市| 本溪市| 绍兴市|