Commit 04960fe2 authored by userresu's avatar userresu

added 2016 material

parent 675dc991
#include <cstdio>
using namespace std;
typedef long long ll;
#define MAXX 1000000000
int Q;
struct node {
int L, R;
node *l, *r;
int cnt;
ll sum;
void add (int i) {
if (L == R) {
++cnt;
sum += L;
return;
}
int M = (L+R)/2;
if (i <= M) {
if (l == NULL) l = new node, *l = (node){L,M,0,0,0,0};
l->add(i);
}
else {
if (r == NULL) r = new node, *r = (node){M+1,R,0,0,0,0};
r->add(i);
}
cnt = sum = 0;
if (l) cnt = l->cnt, sum = l->sum;
if (r) cnt += r->cnt, sum += r->sum;
}
void rem (int i) {
if (L == R) {
--cnt;
sum -= L;
return;
}
int M = (L+R)/2;
if (i <= M) l->rem(i);
else r->rem(i);
cnt = sum = 0;
if (l) cnt = l->cnt, sum = l->sum;
if (r) cnt += r->cnt, sum += r->sum;
}
int getcnt (int i,int j) {
if (i <= L && R <= j) return cnt;
int M = (L+R)/2, ret = 0;
if (i <= M && l) ret += l->getcnt(i,j);
if (j > M && r) ret += r->getcnt(i,j);
return ret;
}
ll getsum (int i,int j) {
if (i <= L && R <= j) return sum;
int M = (L+R)/2;
ll ret = 0;
if (i <= M && l) ret += l->getsum(i,j);
if (j > M && r) ret += r->getsum(i,j);
return ret;
}
int kth (int k) {
if (L == R) return L;
int M = (L+R)/2;
int cntl = 0;
if (l) cntl = l->cnt;
if (k <= cntl) return l->kth(k);
return r->kth(k-cntl);
}
};
int main () {
#ifdef CONTEST
freopen("bicycle.in","r",stdin);
freopen("bicycle.out","w",stdout);
#endif
node T = (node){1,MAXX,0,0,0,0};
int N = 0;
scanf("%d",&Q);
while (Q--) {
int t, x, y;
scanf("%d",&t);
if (t == 1) {
scanf("%d",&x);
++N;
T.add(x);
}
else if (t == 2) {
scanf("%d",&x);
--N;
T.rem(x);
}
else {
scanf("%d %d",&x,&y);
ll k = T.kth((N * y + x + y - 1) / (x+y));
printf("%lld\n", x * (T.getcnt(0,k-1) * k - T.getsum(0,k-1)) + y * (T.getsum(k+1,MAXX+1) - T.getcnt(k+1,MAXX+1) * k));
}
}
return 0;
}
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define MAXN 1000001
int N, A[MAXN], B[MAXN], maxdp[MAXN];
vector<pair<int,int> > mx[MAXN];
pair<int,int> v[MAXN];
bool cmp (const int &a, const int &b) {
return a - A[a] < b - A[b];
}
int main () {
#ifdef CONTEST
freopen("bookeaters.in","r",stdin);
freopen("bookeaters.out","w",stdout);
#endif
scanf("%d",&N);
for (int i=1;i<=N;++i) scanf("%d",&A[i]), B[i] = i;
sort(A+1,A+N+1);
sort(B+1,B+N+1,cmp);
mx[0].push_back(make_pair(0,0));
for (int i=1;i<=N;++i) {
if (A[i] <= i) maxdp[i] = max(maxdp[i-1], maxdp[i-A[i]] + 1);
else maxdp[i] = maxdp[i-1];
}
for (int _i=1,j=1;_i<=N;++_i) {
int i = B[_i];
if (A[i] <= i) {
for (;j<=i-A[i];++j) if (v[j].first) {
while (!mx[v[j].first].empty() && mx[v[j].first].back().first >= v[j].second) mx[v[j].first].pop_back();
mx[v[j].first].push_back(make_pair(v[j].second,j));
}
int dp = maxdp[i-A[i]];
int l = 0, r = mx[dp].size()-1;
while (l < r) {
int m = (l+r)/2;
int a = max(mx[dp][m].first, i - mx[dp][m].second), b = max(mx[dp][m+1].first, i - mx[dp][m+1].second);
if (a < b) r = m;
else if (a > b) l = m+1;
else l = r = m;
}
int get = max(mx[dp][l].first, i - mx[dp][l].second);
if (i == N) printf("%d %d\n", dp + 1, get);
else v[i] = make_pair(dp + 1, get);
}
}
return 0;
}
#include <stdio.h>
#define MAXN 100000
int A[MAXN+1];
int feasible(int k, int N, int D)
{
int i, day=0;
for (i=1; i<=N+D; i++) {
if (i <= N) day+=A[i];
if ((long long) day > (long long) k*(D+1)) return 0;
if ((long long) day > (long long) k*(N+D-i+1)) return 0;
if (day<=k) day=0;
else day-=k;
}
if (!day) return 1;
}
int main()
{
#ifdef CONTEST
freopen("chef.in", "rt", stdin);
freopen("chef.out", "wt", stdout);
#endif
int N, M, D, low, up, mid, x, i;
scanf("%d %d %d", &N, &D, &M);
for (i=1; i<=N; i++) A[i]=0;
for (i=0; i<M; i++) {
scanf("%d", &x); A[x]++;
}
low=1; up=M;
while (low<up) {
mid=(low+up)/2;
if (feasible(mid, N, D)) up=mid;
else low=mid+1;
}
printf("%d\n", low);
return 0;
}
File added
File added
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NILL (-1)
#define INF ((unsigned int) 0x7FFFFFFF)
#define MAXN 50000
#define MAXM 400000
/* Data type definitions for graph representation */
typedef struct _edge {
int u, v, len;
} Edge;
Edge E[MAXM];
typedef struct _node {
int id, len;
struct _node *next;
} Node;
Node *G[MAXN];
typedef struct _nodeinfo {
int id, dist;
} NInfo;
NInfo V[MAXN];
int Loc[MAXN], D[MAXN], Cin[MAXN];
/* ------------------ Fast Read ------------------------ */
#define BSIZE 1<<15
char buffer[BSIZE];
int bpos = 0L, bsize = 0L;
int readInt()
{
int d = 0L, x = 0L;
char c;
while (1) {
if (bpos >= bsize) {
bpos = 0;
if (feof(stdin)) return x;
bsize = fread(buffer, 1, BSIZE, stdin);
}
c = buffer[bpos++];
if (c >= '0' && c <= '9') { x = x*10 + (c-'0'); d = 1; }
else if (d == 1) return x;
}
return -1;
}
/* ------------------ Fast Read ------------------------ */
Node *initNode(int id, int len)
{
Node *v;
if ((v =(Node *)malloc(sizeof(Node))) == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
v->id = id; v->len = len; return(v);
}
void readGraph(int N, int M)
{
Node *new;
int i, x, y, len; //, best=0;
for (i = 0; i < N; i++) G[i] = NULL;
for (i = 0; i < M; i++) {
// scanf("%d %d %d\n", &x, &y, &len);
x = readInt(); y = readInt(); len = readInt();
// if (best < len) best=len;
E[i].u = --x; E[i].v = --y; E[i].len = len;
new = initNode(y, len); new->next = G[x]; G[x] = new;
}
// fprintf(stderr, "(max len = %d)\n", best);
}
/* ------------------------------------------------- */
/* Priority queue implementation using a binary heap */
int heapsize;
void swapNodes(NInfo A[], int Loc[], int i, int j)
{
NInfo tmp = A[i]; A[i] = A[j]; A[j] = tmp;
Loc[A[i].id] = i; Loc[A[j].id] = j;
}
void createHeap(NInfo A[], int Loc[], int N)
{
int i; heapsize = N-1;
for (i = (N-1)/2; i >= 0; i--)
fixHeap(A, Loc, i);
}
int fixHeap(NInfo A[], int Loc[], int i)
{
int left = 2*i+1, right = left+1, smallest = i;
if ((left <= heapsize) && (A[left].dist < A[i].dist)) smallest = left;
if ((right <= heapsize) && (A[right].dist < A[smallest].dist)) smallest = right;
if (smallest != i) {
swapNodes(A, Loc, i, smallest);
fixHeap(A, Loc, smallest);
}
}
int extractMin(NInfo A[], int Loc[])
{
int res = A[0].id;
swapNodes(A, Loc, 0, heapsize);
heapsize--; fixHeap(A, Loc, 0);
return(res);
}
void decreasePriority(NInfo A[], int Loc[], int i)
{
int parent = (i-1)/2;
while ((i > 0) && (A[parent].dist > A[i].dist)) {
swapNodes(A, Loc, i, parent);
i = parent; parent = (i-1)/2;
}
}
int emptyHeap()
{
if (heapsize >=0) return(0);
else return(1);
}
/* End of priority queue implementation */
/* ------------------------------------------------- */
void Dijkstra(int N, int M, int *wcrit, int *crit)
{
Node *v;
int i, u, locu;
int tmp;
for (i = 0; i < N; i++) {
V[i].dist = INF; V[i].id = Loc[i] = i; Cin[i] = 0;
}
V[0].dist = 0;
createHeap(V, Loc, N);
while (!emptyHeap()) {
u = extractMin(V, Loc); locu = Loc[u]; D[u] = V[locu].dist; v = G[u];
// printf("Extracting %3d: (L[%3d] = %d).\n", u, u, V[locu].dist);
while (v != NULL) {
tmp = V[locu].dist + v->len;
if (V[Loc[v->id]].dist > tmp) {
V[Loc[v->id]].dist = tmp;
decreasePriority(V, Loc, Loc[v->id]);
}
v = v->next;
}
}
for (i = 0; i < M; i++) {
if (D[E[i].u] + E[i].len == D[E[i].v]) {
Cin[E[i].v]++; (*wcrit)++;
}
}
for (i = 1; i < N; i++) {
if (Cin[i] == 1) (*crit)++;
if (!Cin[i]) fprintf(stderr, "Node %d unreachable (dist %d)!\n", i, D[i]);
if (D[i] >= INF) fprintf(stderr, "Node %d has infinite (dist %d)!\n", i, D[i]);
}
}
int main()
{
int N, M;
int wc=0, c=0;
#ifdef CONTEST
freopen("critical.in", "rt", stdin);
freopen("critical.out", "wt", stdout);
#endif
scanf("%d %d\n", &N, &M); //fprintf(stderr, "N = %d, M = %d ", N, M);
readGraph(N, M);
Dijkstra(N, M, &wc, &c);
printf("%d %d\n", wc, c);
return 0;
}
#undef DEBUG
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) do ; while(0)
#define NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int xxx (int n, int x[])
{
int q = 0, p = 0, best = 0;
int i;
for (i=n-1; i>=0; i--) {
q -= x[i];
if (q < 0) q = 0;
p += x[i];
if (q == 0 && p > best) best = p;
}
return best;
}
#define MAX 1000000
int n;
int x[MAX];
int main ()
{
#ifdef CONTEST
freopen("metoxes2.in", "rt", stdin);
freopen("metoxes2.out", "wt", stdout);
#endif
int i;
scanf("%d\n", &n);
for (i=0; i<n; i++) scanf("%d", &(x[i]));
printf("%d\n", xxx(n, x));
return 0;
}
#include <cstdio>
#include <cmath>
#include <cassert>
#undef NDEBUG
using namespace std;
#define MAXRT 1000
int cub[MAXRT+1];
int main ()
{
#ifdef CONTEST
freopen("sumofcubes.in", "rt", stdin);
freopen("sumofcubes.out", "wt", stdout);
#endif
for (int i=0; i<=MAXRT; i++)
cub[i] = i*i*i;
int T;
scanf("%d", &T);
for (int t=0; t<T; t++) {
int N;
scanf("%d", &N);
int count = 0;
for (int a=0; 3*cub[a]<=N; a++) {
int c = MAXRT;
for (int b=a; cub[a]+2*cub[b]<=N; b++) {
while (cub[a] + cub[b] + cub[c] > N)
c--;
if (cub[a] + cub[b] + cub[c] == N)
count++;
}
}
printf("%d\n", count);
}
return 0;
}
......@@ -6,6 +6,7 @@
## Παλιότερα έτη
- [2015](home-2015)
- [2014](home-2014)
- [2013](home-2013)
- [2012](home-2012)
......
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