当前位置:网站首页>uva1377
uva1377
2022-07-27 20:09:00 【Stab the bear with a knife】
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 64,maxm = 2100000;
int n,m,input[maxn],mx,vis[maxm];
unordered_map<int, int> mmp;
vector<int> ans;
struct Node {
int state;
set<int> st;
bool isOver(int val) {
return state == val;
}
bool HasFlag(int index) {
return state & (1 << index);
}
vector<int> GetResult() {
vector<int> ret;
for (auto it = st.begin(); it != st.end(); it++) {
ret.push_back(*it);
}
return ret;
}
void AddVal(int input) {
int dis = 0;
for (auto it = st.begin(); it != st.end(); it++) {
dis = abs(*it - input);
if (!mmp.count(dis)) continue;
state |= (1 << mmp[dis]);
}
st.insert(input);
}
};
void bfs() {
queue<Node> q;
Node head;
head.state = 0;
head.st.insert(0);
q.push(head);
int over = (1 << n) - 1;
while (!q.empty()) {
head = q.front(); q.pop();
if (head.isOver(over)) {
ans = head.GetResult();
return;
}
if (head.st.size() >= 7) continue;
for (int i = 0; i < n; i++) {
if (!head.HasFlag(i)) {
for (auto it = head.st.begin(); it != head.st.end(); it++) {
if (*it + input[i] <= mx) {
int nx = *it + input[i];
Node tmp = head;
tmp.AddVal(nx);
if (vis[tmp.state])continue;
vis[tmp.state] = 1;
q.push(tmp);
}
}
}
}
for (int i = 0; i < n; i++) {
if (!head.HasFlag(i)) {
for (auto it = head.st.begin(); it != head.st.end(); it++) {
if (*it - input[i] > 0) {
int nx = *it - input[i];
Node tmp = head;
tmp.AddVal(nx);
if (vis[tmp.state])continue;
vis[tmp.state] = 1;
q.push(tmp);
}
}
}
}
}
}
int main() {
int kCase = 1;
while (cin >> n && n) {
mmp.clear();
for (int i = 0; i < n; i++) cin >> input[i];
sort(input, input + n);
n = unique(input, input + n) - input;
memset(vis, false, sizeof(vis));
mx = input[n - 1];
for (int i = 0; i < n; i++) mmp[input[i]] = i;
ans.clear();
bfs();
int sz = ans.size();
cout << "Case " << kCase++ << ":" << endl << sz << endl;
for (int i = 0; i < sz; i++) {
if (i)cout << " " << ans[i];
else cout << ans[i];
}
cout << endl;
}
return 0;
}
边栏推荐
- Rodin 安装 SMT Solvers 插件
- 连接池-归还连接详解(上)
- uva1377
- JS实现视频录制-以Cesium为例
- Introduction to basic cesium controls
- cesium基本控件介绍
- [C #] positive sequence, reverse sequence, maximum value, minimum value and average value
- Chapter 3 basic operation
- Rodin installs the SMT solvers plug-in
- How to encrypt the data in MySQL database? Mysql8.0 comes with new features
猜你喜欢

ViewUI 中 DatePicker 日期选择器在 IE11 浏览器中兼容解决方案

Common errors reported by pytorch

TS2532: Object is possibly ‘undefined‘

真实案例,大学生接单被骗,希望大家不要被骗了【惨痛教训】

PC博物馆(3) MITS Altair 8800

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

PC Museum (3) MITs Altair 8800

mysql函数汇总之系统信息函数
![[论文阅读] Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation](/img/a9/690f52b5c4afba684f0add2434888c.png)
[论文阅读] Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation

十年测试老鸟聊聊移动端兼容性测试
随机推荐
LED高精度体重秤方案规格书
[paper reading] rich feature hierarchies for accurate object detection and semantic segmentation
sqlite创建表联合主键的sql写法
transformers-bert
Ms721 load test
focal loss
第3章 基本操作
kubectl 获取pod日志 —— 筑梦之路
uva1421
黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
探索新一代活动获客方式,虚拟化活动棋胜一招 | 厂商征集
邬贺铨:因地制宜 数字化技术赋能“双碳”实践
【PyTorch系列】PyTorch之torchvision 图像处理库详解
Cesium常用坐标系详细介绍
Understanding of basic concepts of channel capacity and channel bandwidth
连接池-归还连接详解(上)
【C#网络应用编程】实验3:进程管理练习
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
Function summary
10.31 extended configuration of static route