-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection.cpp
136 lines (130 loc) · 3.75 KB
/
bisection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
using namespace std;
void create(set<int> &zero, set<int> &one, int idx, vector<int> row, queue<pair<int, vector<int>>> &buf) {
if (zero.count(idx)) {
for (auto x: row) {
one.insert(x);
}
}
else if (one.count(idx)) {
for (auto x: row) {
zero.insert(x);
}
}
else {
int which = -1;
for (auto x: row) {
if (zero.count(x)) {
which = 0;
break;
}
else if (one.count(x)) {
which = 1;
break;
}
else continue;
}
if (which == 0) {
one.insert(idx);
for (auto x: row) {
zero.insert(x);
}
}
else if (which == 1) {
zero.insert(idx);
for (auto x: row) {
one.insert(x);
}
}
else {
buf.push({idx, row});
}
}
}
double reproduce(set<int> zero, set<int> one, vector<double> data) {
double sum_zero, sum_one;
for (auto itr=zero.begin(); itr!=zero.end(); ++itr) {
sum_zero += data[*itr];
}
for (auto itr=one.begin(); itr!=one.end(); ++itr) {
sum_one += data[*itr];
}
return abs(sum_zero - sum_one);
}
string output_string(set<int> zero, set<int> one, int n) {
string output = "";
for (int i=0; i<n; i++) {
output += '_';
}
for (auto itr=zero.begin(); itr!=zero.end(); ++itr) {
output[*itr] = '0';
}
for (auto itr=one.begin(); itr!=one.end(); ++itr) {
output[*itr] = '1';
}
return output;
}
double reproduce_from_string(string output, vector<double> data) {
double sum_zero, sum_one;
for (int i=0; i<output.size(); i++) {
if (output[i] == '0') sum_zero += data[i];
else sum_one += data[i];
}
return abs(sum_zero - sum_one);
}
int main(int args, char *argv[]) {
ifstream ifs(argv[1]);
if (ifs.fail()) {
cerr << "Cannot open file\n";
exit(0);
}
string str;
priority_queue<pair<double, int>> q;
double tmp;
int idx = 0;
vector<double> data;
while (getline(ifs, str)) {
stringstream ss(str);
ss >> tmp;
q.push({tmp, idx});
data.push_back(tmp);
idx += 1;
}
pair<double, int> first, second;
vector<vector<int>> table(q.size());
while (q.size() != 1) {
first = q.top(); q.pop();
second = q.top(); q.pop();
q.push({first.first - second.first, first.second});
table[first.second].push_back(second.second);
table[second.second].push_back(first.second);
}
queue<pair<int, vector<int>>> buf;
set<int> zero, one;
zero.insert(0);
for (auto x: table[0]) {
one.insert(x);
}
for (int i=1; i<table.size(); i++) {
create(zero, one, i, table[i], buf);
}
while (!buf.empty()) {
pair<int, vector<int>> tmp = buf.front();
buf.pop();
create(zero, one, tmp.first, tmp.second, buf);
}
cout << "# result: " << fixed << setprecision(20) << q.top().first << endl;
cout << "# " << zero.size() << " " << one.size() << endl;
double repro = reproduce(zero, one, data);
cout << "# repro: " << fixed << setprecision(20) << repro << endl;
string output = output_string(zero, one, data.size());
double repro_str = reproduce_from_string(output, data);
cout << "# repro from str: " << fixed << setprecision(20) << repro_str << endl;
cout << output << endl;
}