661 - Blowing Fuses

All about problems in Volume 6. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Congratulations, you've just started the fifth thread on this problem!
- You just dump your code, without any explanation;
- You don't use code tags, so it will be printed in a proportional letter type, all lines cramped to the left without indentation;
- Your code is completely non-descriptive, using variables a, b, c, etc., and goto label ss.

Do you realy expect anyone to actually help you?
The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

661 - Blowing Fusues! Runtime Error.

Post by linux »

Can anyone help me? :( My code is giving Runtime Error. Here's my code:

Code: Select all

#include<stdio.h>
main()
{
	int n,m,c,i,power_device[21],operations[21],consumption,Max,turn[21],sequence,state;
	for (sequence=1;;sequence++)
	{
		scanf("%d %d %d",&n,&m,&c);
		if (!n && !m && !c) break;
		for (i=1;i<=n;i++)
		{
			scanf("%d",&power_device[i]);
		}
		consumption=0;Max=0;
		for (i=1;i<=20;i++) turn[i]=0;
		for (i=1;i<=m;i++)
		{
			scanf("%d",&operations[i]);
			state=operations[i];
			if (state<=n)
			{
			if (turn[state]==0)
			{
				consumption += power_device[state];
				turn[state]=1;
			}
			else
			{
				turn[state]==0;
				consumption-=power_device[state];
			}
			if (c < Max) break;
			if (Max < consumption) Max = consumption;
			}
		}
		printf ("Sequence %d\n",sequence);
		if (c < Max) printf ("Fuse was blown.\n");
		else
		{
			printf ("Fuse was not blown.\n");
			printf("Maximal power consumption was %d amperes.\n",Max);
		}
	}
	return 0;
}
The OJ tells me:
Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference!
Solving for fun..
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

I've modified source-code! but Giving TLE. Why?

Post by linux »

My modified source-code is

Code: Select all

#include<stdio.h>
main()
{
	int n,m,c,i,power_device[21][2],operations[21],consumption,Max,sequence,state;
	for (sequence=1;;sequence++)
	{
		scanf("%d %d %d",&n,&m,&c);
		if (!n && !m && !c) break;
		for (i=1;i<=n;i++)
		{
			scanf("%d",&power_device[i][1]);
			power_device[i][2]=0;
		}
		consumption=0;Max=0;

		for (i=1;i<=m;i++)
		{
			scanf("%d",&operations[i]);
			state=operations[i];
			if (state<=n && c>Max){
			if (!power_device[state][2])
			{
				consumption += power_device[state][1];
				power_device[state][2]=1;
			}
			else
			{
				power_device[state][2]=0;
				consumption-=power_device[state][1];
			}
			if (Max < consumption) Max = consumption;
			}
		}
		printf ("Sequence %d\n",sequence);
		if (c < Max) printf ("Fuse was blown.\n");
		else
		{
			printf ("Fuse was not blown.\n");
			printf("Maximal power consumption was %d amperes.\n",Max);
		}
	}
	return 0;
}
What's the problem in my code?
Solving for fun..
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Why do you think m, the number of operations, is limited to 20?
Try to make your program independant of m, so it can handle hundreds or thousands of operations.
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

The problem in your code was....

Post by linux »

Got AC. Thanks for all posts.

Code: Select all

Removed
Last edited by linux on Thu Sep 11, 2008 9:41 pm, edited 2 times in total.
Solving for fun..
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

The problem in your code was....

Post by linux »

Hey! frostina, I've solved this program got an error in your code.
You've broken your for-loop when s>c.
Definitely It's your fault. Make a correction and get accepted. Wish you success.
Solving for fun..
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

so. Frostina!

Post by linux »

I don't think there's any problem in your vector. It's quite okay.
Solving for fun..
linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

so. Frostina!

Post by linux »

Thanks to Little joey for his help!
Solving for fun..
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

661

Post by newton »

thank you very much..




Code: Select all


       >> del  forum\\code  

Last edited by newton on Sat Jul 14, 2007 12:58 pm, edited 2 times in total.
Erik
Learning poster
Posts: 67
Joined: Fri Jul 01, 2005 11:29 am
Location: Germany
Contact:

Post by Erik »

Hi,

Code: Select all

if(amp>l)
  max=amp;
l=amp; 
I think you mixed something up there. You don't use max at all. Think it over.

Code: Select all

      if(amp>=c)
         {
         printf("Sequence %ld\nFuse was blown.\n\n",s);
         break;
         } 
The fuse does not blow when the currect reaches c. It blows when the current goes beyond c.
Secondly, according to the break; you don't continue reading the missing gadget-switches. Hence you might get in trouble as you read the next a, b and c but you are still in the middle of gadgets of the previous testcase.

Cu, Erik :)
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Post by newton »

problem solved
Last edited by newton on Sat Jul 14, 2007 1:00 pm, edited 1 time in total.
Erik
Learning poster
Posts: 67
Joined: Fri Jul 01, 2005 11:29 am
Location: Germany
Contact:

Post by Erik »

Hi,

this is a multiple input problem. There are many testcases in it. Hence if you break from the loop that way, you might read wrong data when processing the next testcase.

Cu, Erik :)
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Post by newton »

yeah
i do agee it.
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Post by newton »

Thanx everybody.
there was a chilly mistake.

Code: Select all


                me [spoiler] too


newton...................................................simply the best.
Last edited by newton on Tue Mar 13, 2007 7:33 am, edited 1 time in total.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Try the cases...

Input:

Code: Select all

5 3 9
7
6
6
7
2
3
2
2
3 9 10
5
3
5
3
2
3
3
2
1
1
3
2
0 0 0
Output:

Code: Select all

Sequence 1
Fuse was blown.

Sequence 2
Fuse was not blown.
Maximal power consumption was 10 amperes.
Hope these help.
Ami ekhono shopno dekhi...
HomePage
Post Reply

Return to “Volume 6 (600-699)”