This is Discussion thread about Cover Them All!
Discussion About Cover Them All!
This is my code and I’m getting error in test case, Can any one let me know
#include
using namespace std;
int main() {
int a,b,t,count,ans=0;
cin>>t;
while(t–){
ans=0;
cin>>a>>b;
for(int i=a;i<=b;i++){
count=0;
for(int j=2;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==1){
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
What is the Question ?
The question is https://hack.codingblocks.com/app/practice/3/715/problem , last two test cases do not pass. Which data type should i use??
#include<bits/stdc++.h>
//#define MOD 1000000007
using namespace std;
int main() {
int testcase=0;
cin>>testcase;
for(int t=0;t<testcase;t++){
int n;
cin>>n;
unsigned long long int army[n],bombs[n],cost=0,MOD=1000000007;
for(int i=0;i<n;i++){
cin>>army[i];
}
for(int i=0;i<n;i++){
cin>>bombs[i];
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
int p=army[j]-army[i];
if(p>0){
}
else{
p=-1*p;
}
cost=cost+((p)*max(bombs[i],bombs[j]));
//cout<<i<<" "<<j<<endl;
}
}
cout<<cost%MOD<<endl;
}
}
package HackerBlocks;
import java.util.Scanner;
public class CoverThemAll {
public static void main(String[] args) {
byte times = 0;
Scanner scan=new Scanner(System.in);
if(scan.hasNextInt()) {
times=scan.nextByte();
}
while(times>=1) {
short n=0;
if(scan.hasNextShort())
n=scan.nextShort();//no of soldiers
int first=scan.nextInt();
int a[]=new int[n]; //position of soldiers
short b[]=new short[n]; //no of bombs each soldier has
a[0]=first;
String s1="";
s1=scan.nextLine();
String[] splited=s1.split(" ");
for(int i=1;i<n;i++)
a[i]=Integer.parseInt(splited[i]);
s1="";
if(scan.hasNextLine())
s1=scan.nextLine();
String[] splited2=s1.split(" ");
for(int i=0;i<n;i++)
b[i]=Byte.parseByte(splited2[i]);
int ans=0;
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
int max_value=Math.max(b[i],b[j]);
ans+=Math.abs(a[j]-a[i])*max_value;
}
}
System.out.println(ans);
times--;
}
scan.close();
}
}
//this is my code. it is runnig fine in eclipse but not in coding blocks ide…
it would be great if someone could help
Bhoomika Garg,
You should use class name as Main instead of CoverThemAll
except that do you find any error in code?
No, rest of your code is correct
your code is correct but can give TLE
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();
int count=0;
int[]axis=new int[n];
int[]bomb=new int[n];
for(int i=0;i<n;i++){
axis[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
bomb[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
count+=(Math.abs(axis[i]-axis[j])*Math.max(bomb[i],bomb[j]));
//System.out.println(bomb[j]);
}
}
System.out.println(count);
t–;
}
}
}
Can Anyone tell what’s wrong with this code.??
I’m also facing the same problem last two test case is showing wrong output.
here is my code:
T = eval(input()) while T>0: N = eval(input()) xcord = input() bomb = input() X = [int(x) for x in xcord.split()] B = [int(b) for b in bomb.split()] total = int(0) for i in range(len(X)-1): for j in range(i+1,len(X)): total = total + abs(X[i] - X[j]) * max(B[i],B[j]) print(total) T= T-1
This code is working absolutely fine in my IDE. But not even a single test case is passed when I submit it here. Kindly suggest to me the needed amendments to it.
import java.util.*;
public class Main {
static int cost(int coordinates[], int bombs[], int n){
int totalCost = 0;
for(int i = 0; i < n-1; i++ ){
for(int j = i+1;j < n; j++){
totalCost += Math.abs(coordinates[i] - coordinates[j])*Math.max(bombs[i],bombs[j]);
}
}
return totalCost;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0){
int n = sc.nextInt();
int[] coordinates = new int[n];
int[] bombs = new int[n];
for(int i = 0; i < n; i++)
coordinates[i] = sc.nextInt();
for(int i = 0; i < n; i++)
bombs[i] = sc.nextInt();
System.out.println(cost(coordinates,bombs,n));
}
}
}
I am also facing the same issues. It is running perfectly in other IDE’s but here no test case is getting passed. If changes in logic and algorithm is required I request the author of the problem to share the editorial.
TLE ???
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int int64_t
int32_t main()
{
int t;
cin >> t;
while (t–) {
int n; cin >> n;
int loc[n], bomb[n];
for (int i = 0; i < n; ++i)
{
cin >> loc[i];
}
for (int i = 0; i < n; ++i)
{
cin >> bomb[i];
}
int sum = 0;
for (int i = 0; i < n; ++i)
{
int cost;
for (int j = i + 1; j < n; ++j)
{
// cout << max(bomb[i], bomb[j]) << " " << abs(loc[i] - loc[j]) << " " << endl;
cost = abs(loc[i] - loc[j]) * max(bomb[i], bomb[j]);
sum += cost;
}
}
cout << sum << endl;
}
return 0;
}