Jump to content

[Resolvido] problema das olimpiadas, Invalid Funtion


epolozero
 Share

Recommended Posts

Boas, estou a resolver um problema das olimpiadas ( http://www.dcc.fc.up.pt/oni/problemas/2014/qualificacao/probA.html ) mas esta a dar-me 88 pontos com invalid function, Resolvi o problema com o conceito de buckets (acho...).

#include <stdio.h>


#define MAX_VALUE 1000000
#define MAX_BUCKET 1000 //sqrt(max_value)
unsigned long values[MAX_VALUE] = {0};
unsigned long bucket[MAX_VALUE];
unsigned long sum[MAX_BUCKET] = {0};


int main(void){

  unsigned long n, k, i, j, l, cnt;
 char op[4];

  scanf("%lu", &n);

  //buckets
  for (i=0, j=0; i< MAX_VALUE; i++, j = i / MAX_BUCKET){
	  bucket[i] = j;
  }


  while(n--){
	  scanf("%s", op);
	  scanf("%lu", &k);
	  switch(op[0]){

			 //inserir
			 case 'I':
					values[--k]++;
					sum[bucket[k]]++;
					break;

			 //remover
			 case 'R':
					values[--k]--;
					sum[bucket[k]]--;
					break;

			 //k-esima melhor carta
			 case 'P':
					for (i=MAX_BUCKET-1, j=0; i>=0; i--){
					   j += sum[i];
					   if (j >= k){
							  for (  l = (i + 1) * MAX_BUCKET - 1 , cnt = j - sum[i] ; ; l--){
									 cnt += values[l];
									 if (cnt >= k){
											printf("%lu\n", l+1);
											break;
									 }
							  }
							  break;
					   }
					}
					  break;
	  }
  }

  return 0;
}



Queria que alguem me dissesse o que tenho que melhorar, ou o que estou a fazer errado.

Edited by epolozero
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.