Add problems, solutions and test cases

parent 633fd7de
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define MAX_V 100000
#define COST first
#define A second.first
#define B second.second
#define MT(X,Y,Z) make_pair(X, make_pair(Y, Z))
typedef pair<int, pair<int, int> > iii;
//***** Union Find *****
int comp[MAX_V + 1], rnk[MAX_V + 1];
void init_comp () {
for (int i = 0; i <= MAX_V; i++)
comp[i] = i, rnk[i] = 0;
}
int find (int v) {
if (comp[v] == v) return v;
else return (comp[v] = find(comp[v]));
}
void join (int u, int v) {
u = find(u), v = find(v);
if (rnk[u] < rnk[v]) comp[u] = v;
else comp[v] = u;
if (rnk[u] == rnk[v]) rnk[u]++;
}
// ********************
int main() {
#ifdef CONTEST
freopen("airports.in", "rt", stdin);
freopen("airports.out", "wt", stdout);
#endif
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
vector<iii> E;
for (int i = 0; i < m; i++) {
int city, cost;
scanf("%d %d", &city, &cost);
E.push_back(MT(cost, 0, city));
}
for (int i = 0; i < k; i++) {
int a, b, cost;
scanf("%d %d %d", &a, &b, &cost);
E.push_back(MT(cost, a, b));
}
sort(E.begin(), E.end());
// Allow airport
init_comp();
int total1 = 0;
//int airno = 0;
for (vector<iii>::iterator it = E.begin(); it != E.end(); it++)
if (find(it->A) != find(it->B)) {
join(it->A, it->B);
// if (it->A == 0) airno++;
total1 += it->COST;
}
// Without Airport
init_comp();
int total2 = 0;
for (vector<iii>::iterator it = E.begin(); it != E.end(); it++)
if (find(it->A) != find(it->B) && it->A != 0) {
join(it->A, it->B);
total2 += it->COST;
}
printf("%d\n", min(total1, total2));
return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
#if 0
#define EXPLAIN printf
#else
#define EXPLAIN(...) do {} while(0)
#endif
#define MAX 1000000
int W[MAX];
int main ()
{
#ifdef CONTEST
freopen("elev2.in", "rt", stdin);
freopen("elev2.out", "wt", stdout);
#endif
// read the input
int N, B;
scanf("%d %d", &N, &B);
for (int i=0; i<N; i++)
scanf("%d", &(W[i]));
// sort weights in ascending order
sort(W, W+N);
// calculate
int l = 0, r = N-1;
int count = 0;
while (l <= r) {
// the light ones go only if they fit with the heavy ones
if (l < r && W[l] + W[r] <= B) {
EXPLAIN("%d ", W[l]);
l++;
}
// the heavy ones go anyway
EXPLAIN("%d\n", W[r]);
r--;
count++;
}
printf("%d\n", count);
return 0;
}
File added
File added
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
typedef enum { UNKNOWN=0, HAPPY, UNHAPPY } happiness;
#define MAXN 1000000000
#define MAXS 10000
#define MAXD (9*9*9)
happiness seen[MAXD+1];
int multip[MAXD+1];
int bucket[MAXN/MAXS+1];
struct query {
int A, B;
};
int main ()
{
#ifdef CONTEST
freopen("happy.in", "rt", stdin);
freopen("happy.out", "wt", stdout);
#endif
// precompute numbers up to MAXD
seen[0] = UNHAPPY;
seen[1] = HAPPY;
for (int n=2; n<=MAXD; n++) {
set<int> X;
int k = n;
while (seen[k] == UNKNOWN && X.find(k) == X.end()) {
X.insert(k);
int d1 = k / 100;
int d2 = (k % 100) / 10;
int d3 = k % 10;
k = d1*d1 + d2*d2 + d3*d3;
}
happiness h = (seen[k] == HAPPY) ? HAPPY : UNHAPPY;
for (set<int>::iterator i=X.begin(); i!=X.end(); i++)
seen[*i] = h;
}
// input
int K;
scanf("%d", &K);
query Q[K];
int maxB = 0;
for (int i=0; i<K; i++) {
scanf("%d %d", &(Q[i].A), &(Q[i].B));
if (Q[i].B > maxB) maxB = Q[i].B;
}
// precompute first bucket
for (int i=0; i<MAXS; i++) {
int c = 0;
for (int k=i; k>0; k /= 10) c += (k%10)*(k%10);
multip[c]++;
if (seen[c] == HAPPY) bucket[0]++;
}
// precompute remaining buckets
for (int i=1; i<=maxB/MAXS; i++) {
int c = 0;
for (int k=i; k>0; k /= 10) c += (k%10)*(k%10);
for (int j=0; j<=MAXD-c; j++)
if (seen[j+c] == HAPPY) bucket[i] += multip[j];
}
// answer the queries
for (int i=0; i<K; i++) {
int curr = Q[i].A;
int count = 0;
while (curr % MAXS != 0 && curr <= Q[i].B) {
int c = 0;
for (int k=curr; k>0; k /= 10) c += (k%10)*(k%10);
if (seen[c] == HAPPY) count++;
curr++;
}
while (curr + MAXS <= Q[i].B) {
count += bucket[curr/MAXS];
curr += MAXS;
}
while (curr <= Q[i].B) {
int c = 0;
for (int k=curr; k>0; k /= 10) c += (k%10)*(k%10);
if (seen[c] == HAPPY) count++;
curr++;
}
printf("%d\n", count);
}
return 0;
}
File added
File added
#include <cstdio>
#include <map>
using namespace std;
int main ()
{
#ifdef CONTEST
freopen("maxzero.in", "rt", stdin);
freopen("maxzero.out", "wt", stdout);
#endif
int N;
scanf("%d", &N);
map<int, int> seen;
seen[0] = 0;
int running=0, best=0;
for (int i=0; i<N; i++) {
int a;
scanf("%d", &a);
running += a;
if (seen.find(running) == seen.end())
seen[running] = i+1;
else if (i+1-seen[running] > best)
best = i+1-seen[running];
}
printf("%d\n", best);
return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
#ifdef CONTEST
freopen("metoxes.in", "rt", stdin);
freopen("metoxes.out", "wt", stdout);
#endif
int N, K, money;
scanf("%d %d", &N, &K);
scanf("%d", &money);
int dp[2][K+1][2];
for (int j=0; j<=K; ++j) {
dp[1][j][0] = 0;
dp[1][j][1] = -money;
}
for (int i=2; i<=N; ++i) {
scanf ("%d", &money);
dp[i%2][0][0] = 0;
dp[i%2][0][1] = max(dp[(i-1)%2][0][1], -money);
for (int j=1; j<=K; ++j ) {
dp[i%2][j][0] = max(dp[(i-1)%2][j][0], dp[(i-1)%2][j-1][1] + money);
dp[i%2][j][1] = max(dp[(i-1)%2][j][1], dp[(i-1)%2][j][0] - money);
}
}
printf("%d\n", dp[N%2][K][0]);
}
No preview for this file type
#include <cstdio>
#define MAX 1000000
int count[MAX+1];
using namespace std;
int main ()
{
#ifdef CONTEST
freopen("sumx.in", "rt", stdin);
freopen("sumx.out", "wt", stdout);
#endif
int N, X;
scanf("%d %d", &N, &X);
int pairs = 0;
for (int i=0; i<N; i++) {
int a;
scanf("%d", &a);
if (0 <= X-a && X-a <= MAX) pairs += count[X-a];
count[a]++;
}
printf("%d\n", pairs);
return 0;
}
File added
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment