Contest 3 - Hints to Problems [OFFICIAL]

or multiset can be used, instead of set

For CVOTE wouldnā€™t using a chef class object be better approach with a map of votes for country

Use S.erase(*S.rbegin()) in place of S.erase(S.rbegin()). Here we need to dereference the value pointed by the iterator at rbegin().

If u r using --s.end(), the decrement operator automatically dereference it for us, so no need of *.

Can anyone tell me whats wrong with my code (FENCE Question). Iā€™m getting error RE(SIGSEGV).

#include<stdio.h>
#include<stdlib.h>
int main(void){
int t;
scanf("%d",&t);
while(tā€“){
int N,M;
int K,X[1000]={0},Y[1000]={0},a=1,b,i=1,j=1,ans=0,i1;
scanf("%d %d %d",&N,&M,&K);
for(i1=1;i1<=K;i1++){
scanf("%d %d",&X[i1],&Y[i1]);
}
while(i<=K){
if(X[i]+1==X[j]&&Y[i]==Y[j]){
ans-=1;
j++;
}
if(X[i]==X[j]&&Y[i]+1==Y[j]){
ans-=1;
j++;
}
if(X[i]-1==X[j]&&Y[i]==Y[j]){
ans-=1;
j++;
}
if(X[i]==X[j]&&Y[i]-1==Y[j]){
ans-=1;
j++;
}
if(j==K+1){
j=1;
i++;
ans+=4;
}
else{
j++;
}
}
printf("%d\n",ans);
}
return 0;
}

@sidhant007 Can you please help me with the solution of the last problem of the STL question in DSA learning series. Why one of my test case is giving TLE? Please help !!

Link to the question

Link to my solution

what if A={1,2,3,4,5} B={1,2,2 ,3,5} in this case if you take max from a which is at index 4 and you will make pair with B, then it will produce duplicate sum because B has duplicates

ok I got it, I didnā€™t read that A and B are pairwise distinct

I was solving this question with my submission being code, I am not able to identify what is causing TLE for the 3rd test case of the 2 nd subtask , I cross-checked it with other solutions as well, can someone help me identify the mistake.
@sidhant007 ?

Hey, i also have the same problem. did your problem gave a complete ac?

PBDS didnā€™t. I used a different approach for 100 pts.

1 Like

thanks for replying

For DPAIRS, if I solve according to hints the solution is accepted but suppose If have repeated numbers in A, B i.e(A=1,2,2,3 B=1,7,8) A1+B2<A2+B2 is wrong. Anything I might be missing?

Python programmers, Iā€™ve been having TLE error for the EXUNC problem. Iā€™ve tried looking for other successful submissions but almost all of them TLE errors for the second sub-task.
Hereā€™s my code. Is there any way to improve upon it and make it pass the second subtask?

n, q = map(int, input().split())
l = list(map(int, input().split()))

s = {1}
prev = l[0]
for i in range(1, n):
    if l[i] % prev == 0:
        pass
    else:
        s.add(i+1)
    prev = l[i]

for _ in range(q):
    temp = list(map(int, input().split()))
    if len(temp) == 3:
        i = temp[1]
        num = temp[2]
        s.add(i)
        s.add(i+1)
        
        if i!=1 and num % l[i-2] == 0:
            s.discard(i)
        if i!= n and l[i] % num == 0:
            s.discard(i+1)
        
        l[i-1] = num
    
    elif len(temp) == 2:
        req = temp[1]
        while req > 0:
            if req in s:
                print(req)
                break
            req -= 1

Hi guys,

I am getting Wrong_Answer for this solution of Save Konoha. Can someone please point what is wrong here:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		
			try {
			Scanner input = new Scanner(System.in);
			int testCases = Integer.parseInt(input.nextLine());
		
			while (testCases-- > 0) {
				String[] input1 = input.nextLine().split(" ");
				int N = Integer.parseInt(input1[0]), Z = Integer.parseInt(input1[1]);
				String[] powers = input.nextLine().split(" ");
				
				// max-heap
				PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
				for (int i = 0; i < N; i++) 
					pq.offer(Integer.parseInt(powers[i]));
				
				int hits = 0, pow = 0;
				while (Z > 0 && !pq.isEmpty()) {
					pow = pq.poll();
					Z -= pow;
					
					if (pow/2 != 0) pq.offer(pow/2);
					
					hits++;
				}
				
				if (Z > 0)
					System.out.print("Evacuate");
				else
					System.out.println(hits);
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
} // main
}

Thanks!

hello sir,
I used vectors for solving the question i know it is not efficient.But I am not able to understand what is the error in the code

#include
#include
#include
using namespace std;
int main()
{
long long int tc,soldier_cnt,ele,oppnt_power;
cin>>tc;
while(tcā€“)
{
cin>>soldier_cnt>>oppnt_power;
vectorpower;
for(int i=0;i<soldier_cnt;i++)
{
cin>>ele;
power.push_back(ele);
}
long long int index=max_element(power.begin(),power.end())-power.begin();
long long int value=power[index];
long long int cnt=1,flag=1;
oppnt_power-=value;
power[index]=value/2;
while(oppnt_power > 0)
{
index=max_element(power.begin(),power.end())-power.begin();
value=power[index];
if( value <= oppnt_power)
{
flag=0;
cout<<ā€œEvacuate\nā€;
break;
}
oppnt_power-=value;
power[index]=value/2;
cnt++;
}
if(flag)
cout<<cnt<<"\n";
}
return 0;
}

I need help with the following code
t=int(input())
for z in range(t):
values=input().split(" ā€œ)
n=int(values[0])
count=0
power=int(values[1])
villager=input().split(ā€ ")
for i in range(n):
villager[i]=int(villager[i])

while power>0 and sum(villager)>0:
    attacker=max(villager)
    index=villager.index(attacker)
    power = power - attacker
    villager[index]=villager[index]//2
    count=count+1
    # print(count,villager)
if power>0:
    print("Evacuate")
else:
    print(count)

It is giving runtime error.
Thanks in advance

Hello sir, I am facing aproblem in SAVKONO CodeChef: Practical coding for everyone
I used vector but on submission I am always getting Wrong answer . Will you please tell me loop hole or error in my code?

@sidhant007 Thanks for the hack! But there is some typos in your code which I came across and also to pass some more cases we have to add one more condition which i have updated in the while loop condition here is the code :slight_smile:

       set<pair<ll, ll>> S;
       for(ll i = 1; i <= n; i++) {
           ll a; 
           cin >> a;
           S.insert({a, i});
       }
       ll count = 0;
       while(z > 0 && (*S.rbegin()).first > 0) {
           pair<ll, ll> temp = (*S.rbegin());
           z -= temp.first;
           S.erase(*S.rbegin());
           S.insert({temp.first / 2, temp.second});
           count++;
       }
       if(z > 0) {
           cout << "Evacuate\n";
       } 
       else {
           cout << count << "\n";
       }

its now visible and i cant unsee it now lol

you can use multiset instead, but insertion in multiset is O(n) in worst case and insertion in priority queue is O(logn). (for each element wise)