Skip to content

Instantly share code, notes, and snippets.

@brycetsao
Last active December 12, 2015 04:46
Show Gist options
  • Save brycetsao/0b45102bcb57b7609b06 to your computer and use it in GitHub Desktop.
Save brycetsao/0b45102bcb57b7609b06 to your computer and use it in GitHub Desktop.
CP_mid2
#include <iostream>
#include <algorithm>
using namespace std;
unsigned long long int a[100001] = {}, n, s, t;
unsigned long long int f()
{
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; ++i)
{
a[i] += a[i - 1];
}
return a[t] - a[s - 1];
}
int main(int argc, char const *argv[])
{
int T;
cin >> T;
while(T--)
{
cin >> n >> s >> t;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
}
cout << f() << endl;
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int a[100001], n;
stack<int> s;
bool f()
{
for(int i = 1, j = 1; j <= n;)
{
if(s.empty() || s.top() != a[j])
{
if(i > n) return false;
s.push(i);
//cout << "push " << i << endl;
++i;
}
else
{
//cout << "pop " << s.top() << endl;
s.pop();
++j;
}
}
return true;
}
int main(int argc, char const *argv[])
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
}
cout << (f() ? "yes" : "no") << endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int a[21][21] = {}, p[21], n;
int g(int i)
{
int& x = p[i];
int& y = p[i + 1];
int& z = p[i + 2];
return a[x][y] + a[y][z] + a[z][x];
}
int f()
{
for(int i = 0; i < n; ++i)
{
p[i] = i;
}
int res = 300;
do
{
int sum = 0;
for(int i = 0; i < n; i += 3)
{
sum += g(i);
}
res = min(res, sum);
} while(next_permutation(p, p + n));
return res;
}
int main(int argc, char const *argv[])
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << f() << endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 8400;
int a[21][21] = {}, p[21], n;
int g(int i)
{
int& x = p[i];
int& y = p[i + 1];
int& z = p[i + 2];
return a[x][y] + a[y][z] + a[z][x];
}
void swap(int x, int y)
{
int tmp = p[x];
p[x] = p[y];
p[y] = tmp;
}
int f(int l, int r)
{
if(l + 3 > r) return g(l);
int res = INF;
for(int i = l + 1; i < r; ++i)
{
swap(l + 1, i);
for(int j = i + 1; j < r; ++j)
{
swap(l + 2, j);
int tmp = g(l) + f(l + 3, r);
//cout << '\t' << g(l) + tmp << endl;
if(tmp < res) res = tmp;
swap(l + 2, j);
}
swap(l + 1, i);
}
return res;
}
int main(int argc, char const *argv[])
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
for(int i = 0; i < n; ++i)
{
p[i] = i;
}
cout << f(0, n) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment