Skip to content

Instantly share code, notes, and snippets.

@zhrkvl
Last active December 5, 2016 07:54
Show Gist options
  • Save zhrkvl/68709c7130a771f02d3dac13672936f8 to your computer and use it in GitHub Desktop.
Save zhrkvl/68709c7130a771f02d3dac13672936f8 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define INF (1<<30)
#define INFll (1ll<<62)
#define F first
#define S second
#define MOD 1000000007
#define mkp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(I, A, B) for(int (I) = (A); (I) < (B); (I)++)
#define ROF(I, A, B) for(int (I) = (A); (I) >= (B); (I)--)
#define SQR(A) 1ll*(A)*(A)
const char array_sep[] = " ";
const char array_end[] = "";
const char pair_sep[] = " ";
const char pair_end[] = "";
const char map_sep[] = "->";
const char map_end[] = "\n";
//const int dx[] = {-1, 0, 1, 0};
//const int dy[] = {0, 1, 0, -1};
const int dx[] = {0, 1};
const int dy[] = {1, 0};
const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
template<typename A> ostream & operator<<(ostream & os, const vector<A> & x)
{
for(int i = 0; i < x.size(); i++)
os << x[i] << array_sep;
os << array_end;
return os;
}
template<typename A> ostream & operator<<(ostream & os, const set<A> & x)
{
for(auto& y: x)
os << y << " ";
return os;
}
template<typename A, typename B> ostream & operator<<(ostream & os, const pair<A, B> & x)
{
os << x.first << pair_sep << x.second << pair_end;
return os;
}
template<typename A> istream & operator>>(istream & is, vector<A> & x)
{
for(int i = 0; i < x.size(); i++)
is >> x[i];
return is;
}
template<typename A, typename B> istream & operator>>(istream & is, pair<A, B> & x)
{
is >> x.first >> x.second;
return is;
}
template<typename _key, typename _val> ostream & operator<<(ostream & os, map<_key, _val> & mp)
{
os << "{\n";
for(auto it : mp) // not for C++98 or earlier
os << "\t" << it.F << map_sep << it.S << map_end;
os << "}\n";
return os;
}
template<typename _response> void die(_response ans)
{
cout << ans << endl;
exit(0);
}
string toS(int x)
{
string res = "";
if(!x)
return "0";
while(x)
{
res += '0' + (x % 10);
x /= 10;
}
reverse(res.begin(), res.end());
return res;
}
string compile(vector<int>& a)
{
// cerr << a << endl;
string buf;
buf += toS(a[0]) + "=";
buf += toS(a[1]);
for(int i = 2; i < a.size(); i++)
buf += "+" + toS(a[i]);
// cerr << buf << endl;
return buf;
}
string best(vector<int> &a, vector<int > &b)
{
int res = 0;
for(int i = 0; i < min(a.size(), b.size()); i++)
{
if(a[i] < b[i])
{
res = -1;
break;
}
if(a[i] > b[i])
{
res = 1;
break;
}
}
return (res == -1) ? compile(a) : compile(b);
}
int main()
{
// srand((unsigned int) time(0));
ios_base::sync_with_stdio(0);
// cout << fixed << setprecision(8) << endl;
//#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// freopen("errlog.log", "w", stderr);
//#else
//// freopen("next.in", "r", stdin);
//// freopen("next.out", "w", stdout);
//// freopen("friends.in", "r", stdin);
//#endif
string s;
cin >> s;
string a = s.substr(s.find('=') + 1);
string buf;
vector<int > dat;
for(int i = 0; i < a.size(); i++)
{
if(a[i] == '+')
{
if(buf.size())
dat.push_back(atoi(buf.data()));
buf.clear();
continue;
}
buf += a[i];
}
vector<vector<int > > ans(2);
if(buf.size())
dat.push_back(atoi(buf.data()));
for(int i = dat.size() - 1; i > 0; i--)
{
if(dat[i-1] + 1 <= dat[i] - 1)
{
dat[i]--;
dat[i-1]++;
bool beat = false;
// cerr << dat << endl;
// cerr << mkp(int(beat), i) << endl;
ans[0].push_back(accumulate(all(dat), 0));
ans[0].push_back(dat[0]);
for(int j = 1; j < i; j++)
ans[0].push_back(dat[j]);
int sum = accumulate(dat.begin() + i, dat.end(), 0);
while(sum > 0)
{
if(sum >= (dat[i-1] << 1))
{
sum -= dat[i-1];
ans[0].push_back(dat[i-1]);
continue;
}
ans[0].push_back(sum);
sum = 0;
}
dat[i]++;
dat[i-1]--;
break;
}
}
if(dat.size() == 1)
die("No solution");
dat[dat.size() - 2] += dat[dat.size() - 1];
dat.pop_back();
ans[1].push_back(accumulate(all(dat), 0));
ans[1].push_back(dat[0]);
for(int i = 1; i < dat.size(); i++)
ans[1].push_back(dat[i]);
// cerr << ans[1] << endl;
cout << best(ans[0], ans[1]) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment