Advertisement
a53

Intergalactic

a53
Aug 16th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ///complexitate O(vmax*log(vmax)+n*log(n)+T*log(vmax)*n)
  2. #include <fstream>
  3. #include <algorithm>
  4. using namespace std;
  5. ifstream fin("intergalactic.in");
  6. ofstream fout("intergalactic.out");
  7. int T, n, k, a[200002], b[200002], aux, p, q, i, j, *x, *y, z;
  8. char ciur[2000002];
  9. long long f(int s){
  10. ///calculeaza cate perechi au suma <=s
  11. long long nr=0;
  12. int i,j1,j2,js=q;
  13. for(i=1;i<=p && js>0;i++){
  14. if(i>=2 && x[i]-x[i-1]<100000){
  15. while(js>0 && x[i]+y[js]>s)js--;
  16. }
  17. else{
  18. j1=1; j2=js; js=0;
  19. while(j1<=j2){
  20. j=(j1+j2)/2;
  21. if(x[i]+y[j]<=s){
  22. js=j;
  23. j1=j+1;
  24. }
  25. else{
  26. j2=j-1;
  27. }
  28. }
  29. }
  30. nr=nr+js;
  31. }
  32. return nr;
  33. }
  34. int main(){
  35. ciur[1]=1;
  36. for(i=2;i*i<=2000000;i++){
  37. if(ciur[i]==0){
  38. for(j=i*i;j<=2000000;j=j+i){
  39. ciur[j]=1;
  40. }
  41. }
  42. }
  43. fin>>n>>T;
  44. p=0;q=0;
  45. for(i=1;i<=n;i++){
  46. fin>>z;
  47. if(ciur[z]==0){
  48. p++;a[p]=z;
  49. }
  50. else{
  51. q++;b[q]=z;
  52. }
  53. }
  54. sort(a+1,a+1+p);
  55. sort(b+1,b+1+q);
  56. x=a; y=b;
  57. if(p>q){x=b;y=a;aux=p;p=q;q=aux;}
  58. for(int t=1;t<=T;t++){
  59. fin>>k;
  60. long long k1;
  61. int s1,s2,s3=0,s;
  62. s1=x[1]+y[1]; s2=x[p]+y[q];
  63. while(s1<=s2){
  64. s=(s1+s2)/2;
  65. k1=f(s);
  66. if(k1>=k){
  67. s3=s;
  68. s2=s-1;
  69. }
  70. else{
  71. s1=s+1;
  72. }
  73. }
  74. fout<<s3<<"\n";
  75. }
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement