158 - Calendar

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

Moderator: Board moderators

j0rdi
New poster
Posts: 6
Joined: Sun May 04, 2003 10:52 am

help

Post by j0rdi » Tue May 06, 2003 6:08 pm

i have seen this message, but i continous in the same situation. Here is my code in C. If someone find the error i will thanks.
[c]#include <stdio.h>

struct event
{
int day;
int month;
int importance;
char n_event[255];
};

struct eday
{
int day;
int month;
};

struct line_out
{
char line[270];
int n_stars;
int ddays;
};


int comparacion(struct line_out l1,struct line_out l2)
{

if(l1.ddays<l2.ddays)
{
return(-1);
}
else if(l1.ddays>l2.n_stars)
{
return(1);
}
else
{
if(l1.n_stars>l2.n_stars)
{
return(-1);
}
else
{
if(l1.n_stars<l2.n_stars)
{
return(1);
}
else
{
return(0);
}
}
}
}



void ordena(struct line_out lo1[100],int nelems)
{
int i,j,menor;
struct line_out aux;
for(i=0;i<=nelems;i++)
{
for(j=i+1,menor=i;j<=nelems;j++)
{
if(comparacion(lo1[j],lo1[menor])==-1)
{
menor=j;
}
}

aux.n_stars=lo1.n_stars;
aux.ddays=lo1.ddays;
strcpy(aux.line,lo1.line);
lo1.n_stars=lo1[menor].n_stars;
lo1.ddays=lo1[menor].ddays;
strcpy(lo1.line,lo1[menor].line);
lo1[menor].n_stars=aux.n_stars;
lo1[menor].ddays=aux.ddays;
strcpy(lo1[menor].line,aux.line);
}

}


main (int argc, char *argv[])
{

char line[255];
int year;
int bisiesto=0;
struct event t_events[100];
struct eday t_edays[100];
struct line_out t_lineout[100];
struct line_out t_lineout_ord[100];
char aux[270];
int indx;
int indx2;
int k;
int k2;
int dif;
int i=0;
int j=0;
int nelems=0;
int nin=0;

while (gets(line) !=NULL & line[0]!='#' )
{
if (line[0]=='A')
{
sscanf(line,"A %d %d %d %n",&t_events.day,&t_events.month,&t_events.importance,&indx);
for(k=indx;k<=strlen(line);k++)
{
t_events.n_event[k-indx]=line[k];
}
i++;
}
else
{
if (line[0]=='D')
{
sscanf(line,"D %d %d",&t_edays[j].day,&t_edays[j].month);
j++;
}
else
{
if (line[0]=='1')
{
sscanf(line,"%d",&year);
if (year>1999 || year<1901)
{

exit(1);
}
if (year%4==0)
{
bisiesto=1;
}
}
}
}


}

for(indx=0;indx<=j-1;indx++)
{
printf("Today is: %d %d\n",t_edays[indx].day,t_edays[indx].month);
nin=0;
indx2=0;
while(indx2<=i-1)
{

if (t_edays[indx].month == t_events[indx2].month && t_edays[indx].day <= t_events[indx2].day)
{
dif=t_events[indx2].day - t_edays[indx].day;

}
else
{
if ((t_events[indx2].month - t_edays[indx].month -1 ==0) )
{
if (t_edays[indx].month==4 || t_edays[indx].month==6 || t_edays[indx].month==9 || t_edays[indx].month==11)
{
dif=(t_events[indx2].day+30) - t_edays[indx].day;
}
else
{
if(t_edays[indx].month==2 && bisiesto==1)
{
dif=(t_events[indx2].day+29) - t_edays[indx].day;
}
else
{
if(t_edays[indx].month==2 && bisiesto==0)
{
dif=(t_events[indx2].day+28) - t_edays[indx].day;
}
else
{
dif=(t_events[indx2].day+31) - t_edays[indx].day;
}
}
}
}
else
{
dif=8;
}
}

if (dif<=t_events[indx2].importance)
{

if(t_edays[indx].month==t_events[indx2].month & t_edays[indx].day==t_events[indx2].day)
{
printf("%3d%3d *TODAY* %s\n",t_events[indx2].day,t_events[indx2].month,t_events[indx2].n_event);
}
else
{
sprintf(t_lineout[nin].line,"%3d%3d ",t_events[indx2].day,t_events[indx2].month);
for(k=0;k<7;k++)
{
if(k<(t_events[indx2].importance - dif +1))
{
strcat(t_lineout[nin].line,"*");
}
else
{
strcat(t_lineout[nin].line," ");
}
}
sprintf(aux," %s",t_events[indx2].n_event);
strcat(t_lineout[nin].line,aux);
t_lineout[nin].n_stars=t_events[indx2].importance - dif +1;
t_lineout[nin].ddays=dif;
nelems++;
nin++;
}
}

indx2++;
}
ordena(t_lineout,nelems-1);
for(k=0;k<nelems;k++)
{
puts(t_lineout[k].line);
}
nelems=0;
printf("\n");
}


}



[/c]

j0rdi
New poster
Posts: 6
Joined: Sun May 04, 2003 10:52 am

Post by j0rdi » Tue May 06, 2003 6:10 pm

the problem is number 158-Calendar

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

158 - Calendar - WA

Post by UFP2161 » Wed Aug 13, 2003 2:43 am

Anyone have tricky inputs for this? I know the year can wrap around and have taken that into consideration. If I am reading the question correctly, the events after the requested date are first sorted by the date that they happen, and then by the number of stars (though that's contradictory to another post here).

Also, should the date on the "Today is" line be space delimited, or fit into a width of size 3?

I also handle leap years correctly. So I don't think that's a problem:

Input:

Code: Select all

1991
A 23 2 4 February 23rd
A  1 3 7 March 1st
A  2 3 7 March 2nd
D 23 2
#
Output:

Code: Select all

Today is: 23 2
 23  2 *TODAY* February 23rd
  1  3 **      March 1st
  2  3 *       March 2nd
Input:

Code: Select all

1992
A 23 2 4 February 23rd
A  1 3 7 March 1st
A  2 3 7 March 2nd
D 23 2
#
Output:

Code: Select all

Today is: 23 2
 23  2 *TODAY* February 23rd
  1  3 *       March 1st
Trying real hard not to post code =)

shuniu
New poster
Posts: 34
Joined: Thu Oct 16, 2003 6:15 pm

i finally figured it out

Post by shuniu » Tue Oct 28, 2003 6:15 am

This problem is not very clearly specified, I tried many possible interpretations before getting accepted. And it took me a long time to get rid of a PE, because i didnt realize the date after Today is: must also be right aligned in a field of 3.
The order is by date, then by priority displayed, then by input order. Notice if two events on the same day have different priority, when asked for that day, these events should be ordered by their input order, since the priority displayed are both *TODAY*.

Try this out:

Code: Select all

1991
A 23 12 5 Partner's birthday
A 25 12 7 Christmas
A 20 12 1 Unspecified Anniversary
A 25 12 7 Christmas So Important
A 26 12 6 Boxing Day
A 26 12 6 Christmas Eve + 2
A 1 1 6 New Year
A 31 12 6 New Year Eve
A 1 1 7 New Year Again
A 2 1 4 Nothing
A 1 3 4 First Day of March
A 28 2 4 Feb Regular
D 26 2
D 27 2
D 28 2
D 1 3
D 2 3
D 3 3
D 20 12
D 21 12
D 22 12
D 23 12
D 24 12
D 25 12
D 26 12
D 27 12
D 28 12
D 29 12
D 30 12
D 31 12
D 1 1
D 1 2
#
The result should be:

Code: Select all

Today is: 26  2
 28  2 ***     Feb Regular
  1  3 **      First Day of March

Today is: 27  2
 28  2 ****    Feb Regular
  1  3 ***     First Day of March

Today is: 28  2
 28  2 *TODAY* Feb Regular
  1  3 ****    First Day of March

Today is:  1  3
  1  3 *TODAY* First Day of March

Today is:  2  3

Today is:  3  3

Today is: 20 12
 20 12 *TODAY* Unspecified Anniversary
 23 12 ***     Partner's birthday
 25 12 ***     Christmas
 25 12 ***     Christmas So Important
 26 12 *       Boxing Day
 26 12 *       Christmas Eve + 2

Today is: 21 12
 23 12 ****    Partner's birthday
 25 12 ****    Christmas
 25 12 ****    Christmas So Important
 26 12 **      Boxing Day
 26 12 **      Christmas Eve + 2

Today is: 22 12
 23 12 *****   Partner's birthday
 25 12 *****   Christmas
 25 12 *****   Christmas So Important
 26 12 ***     Boxing Day
 26 12 ***     Christmas Eve + 2

Today is: 23 12
 23 12 *TODAY* Partner's birthday
 25 12 ******  Christmas
 25 12 ******  Christmas So Important
 26 12 ****    Boxing Day
 26 12 ****    Christmas Eve + 2

Today is: 24 12
 25 12 ******* Christmas
 25 12 ******* Christmas So Important
 26 12 *****   Boxing Day
 26 12 *****   Christmas Eve + 2

Today is: 25 12
 25 12 *TODAY* Christmas
 25 12 *TODAY* Christmas So Important
 26 12 ******  Boxing Day
 26 12 ******  Christmas Eve + 2
 31 12 *       New Year Eve
  1  1 *       New Year Again

Today is: 26 12
 26 12 *TODAY* Boxing Day
 26 12 *TODAY* Christmas Eve + 2
 31 12 **      New Year Eve
  1  1 **      New Year Again
  1  1 *       New Year

Today is: 27 12
 31 12 ***     New Year Eve
  1  1 ***     New Year Again
  1  1 **      New Year

Today is: 28 12
 31 12 ****    New Year Eve
  1  1 ****    New Year Again
  1  1 ***     New Year

Today is: 29 12
 31 12 *****   New Year Eve
  1  1 *****   New Year Again
  1  1 ****    New Year
  2  1 *       Nothing

Today is: 30 12
 31 12 ******  New Year Eve
  1  1 ******  New Year Again
  1  1 *****   New Year
  2  1 **      Nothing

Today is: 31 12
 31 12 *TODAY* New Year Eve
  1  1 ******* New Year Again
  1  1 ******  New Year
  2  1 ***     Nothing

Today is:  1  1
  1  1 *TODAY* New Year
  1  1 *TODAY* New Year Again
  2  1 ****    Nothing

Today is:  1  2

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Fri Oct 31, 2003 7:56 am

Blah, it was an input parsing problem. Thanks for the inputs though =)

El-idioto
New poster
Posts: 45
Joined: Mon Jul 14, 2003 9:42 pm
Location: Zoetermeer, The Netherlands

Post by El-idioto » Thu Mar 18, 2004 12:50 am

I have the same output for the inputs stated above, but still get WA.

Could someone with an AC program check my program?
Alternatively, if you have an AC program, please send me the (Windows) executable.[cpp]// @JUDGE_ID: 33444KK 158 C++
//
// 158_Calendar: Show today's events and the events which are in the near future
//

#include <cstdio>

// The base types
#ifdef WIN32
typedef __int8 int8;
typedef __int16 int16;
typedef __int32 int32;
typedef __int64 int64;
typedef unsigned __int8 uint8;
typedef unsigned __int16 uint16;
typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;
#else
typedef char int8;
typedef short int16;
typedef long int32;
typedef long long int int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef unsigned long long int uint64;
#endif

// These macros safely delete (an array of) objects
#define SAFE_DELETE(pObj) { if(pObj) { delete pObj; pObj = NULL; } }
#define SAFE_DELETE_ARRAY(pArr) { if(pArr) { delete[] pArr; pArr = NULL; } }

// The specification of a calendar event
struct EVENT
{
uint16 u16EventId; // The unique ordinal number

uint16 u16Day; // The day of the month
uint16 u16Month; // The month
uint16 u16Importance; // The relative importance [1, 7]
int8 pi8Description[256]; // The description of the event

EVENT *pPrev; // The previous event of the day
EVENT *pNext; // The next event of the day
};

// The amount of days passed after each month (for normal years and for leap years)
static uint16 DaysSoFar[2][13] =
{
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};


// Function prototypes
void AddEvent(EVENT *pEvent);
void ShowEvents(uint16 u16Day, uint16 u16Month);

// Global variables
bool g_bLeapYear;
EVENT *g_ppEvents[366] = {};


//---------------------------------------------------------------------
// F U N C T I O N S
//---------------------------------------------------------------------


// This function is the entrance of the app
int main()
{
uint16 u16Year;

// Get the year
if(scanf("%hu", &u16Year) == 1)
{
int8 pi8Type[2];
uint16 u16EventId = 1;

// Do we have a leap year?
g_bLeapYear = !(u16Year % 4);

// ---------------------
// Read the events
// ---------------------

// Add the next event
while(scanf("%s", pi8Type) == 1 && pi8Type[0] == 'A')
{
EVENT *pEvent;
int8 pi8Desc[256];
uint16 u16Src = 0, u16Dst = 0;

// Allocate a new event
pEvent = new EVENT;
pEvent->u16EventId = u16EventId++;

// Get the details
scanf("%hu %hu %hu", &pEvent->u16Day, &pEvent->u16Month, &pEvent->u16Importance);
fgets(pi8Desc, 256, stdin);

// Get the description
while(pi8Desc[u16Src] == ' ')
u16Src++;
while(pi8Desc[u16Src] != '\0' && pi8Desc[u16Src] != '\n' && pi8Desc[u16Src] != '\r')
pEvent->pi8Description[u16Dst++] = pi8Desc[u16Src++];
pEvent->pi8Description[u16Dst] = '\0';

// Add the event
AddEvent(pEvent);
}

// --------------------
// Read the dates
// --------------------

// Do we have a date?
if(pi8Type[0] == 'D')
{
bool bFirst = true;
uint16 u16Month, u16Day;

do
{
// Show the events for the date
if(scanf("%hu %hu", &u16Day, &u16Month) == 2)
{
// Separate the days
if(bFirst)
bFirst = false;
else
printf("\n");

// Show the events for the day
ShowEvents(u16Day, u16Month);
}
}while(scanf("%s", pi8Type) == 1 && pi8Type[0] == 'D');
}

// ------------------------
// Free the resources
// ------------------------

// Free the resources
for(uint16 u16DayNr=0;u16DayNr<366;u16DayNr++)
{
EVENT *pEvent = g_ppEvents[u16DayNr];

// Delete all the events of the day
while(pEvent)
{
EVENT *pEventNext = pEvent->pNext;

// Delete the event
SAFE_DELETE(pEvent);
pEvent = pEventNext;
}
}
}

return 0;
}


//---------------------------------------------------------------------


// This function adds an event to the calendar
void AddEvent(EVENT *pNewEvent)
{
uint16 u16DayNr;

// Get the event's day number
u16DayNr = uint16(DaysSoFar[g_bLeapYear][pNewEvent->u16Month-1] + pNewEvent->u16Day - 1);

// Don't we have any events yet?
if(g_ppEvents[u16DayNr] == NULL)
{
// Set the first event of the day
pNewEvent->pPrev = NULL;
pNewEvent->pNext = NULL;
g_ppEvents[u16DayNr] = pNewEvent;
}
else
{
EVENT *pEventTemp = g_ppEvents[u16DayNr];

// Don't we have a more important event than the most important event of the day?
if(pNewEvent->u16Importance <= pEventTemp->u16Importance)
{
// Find the insertion point
while(pEventTemp->pNext && pEventTemp->pNext->u16Importance >= pNewEvent->u16Importance)
pEventTemp = pEventTemp->pNext;
}

// Should we insert the new event before the existing event?
if(pNewEvent->u16Importance > pEventTemp->u16Importance)
{
// Insert the new event
if(pNewEvent->pPrev = pEventTemp->pPrev)
pNewEvent->pPrev->pNext = pNewEvent;
pNewEvent->pNext = pEventTemp;
pEventTemp->pPrev = pNewEvent;
}
else
{
// Our event is the least important event of the day
pNewEvent->pPrev = pEventTemp;
pNewEvent->pNext = NULL;
pEventTemp->pNext = pNewEvent;
}

// Make sure we're still pointing to the first event
while(g_ppEvents[u16DayNr]->pPrev)
g_ppEvents[u16DayNr] = g_ppEvents[u16DayNr]->pPrev;
}
}


//---------------------------------------------------------------------


// This function shows the events in the coming days
void ShowEvents(uint16 u16Day, uint16 u16Month)
{
uint16 u16DaysThisYear = (g_bLeapYear ? 366 : 365), u16DayNr;

// Show which day it is today
printf("Today is: %2u %2u\n", u16Day, u16Month);

// --------------------
// Today's events
// --------------------

// Get the day nr
u16DayNr = DaysSoFar[g_bLeapYear][u16Month-1] + u16Day - 1;

// Do we have any events today?
if(g_ppEvents[u16DayNr])
{
uint16 u16MinimumId = 0;

// Show today's events in order of input
for(;;)
{
EVENT *pCurEvent = g_ppEvents[u16DayNr], *pEvent = NULL;
uint16 u16LowestId = 65535;

// Check the events
while(pCurEvent)
{
// Do we have a new minimum event (but higher than the previous one)?
if(pCurEvent->u16EventId < u16LowestId && pCurEvent->u16EventId > u16MinimumId)
{
// We have the lowest so far
pEvent = pCurEvent;
u16LowestId = pEvent->u16EventId;
}

// Go to the next event
pCurEvent = pCurEvent->pNext;
}

// Do we have a lowest event?
if(pEvent)
{
// Show the event
printf(" %2u %2u *TODAY* %s\n", pEvent->u16Day, pEvent->u16Month, pEvent->pi8Description);
u16MinimumId = pEvent->u16EventId;
}
else
break;
}
}

// ------------------------------
// Events for coming 7 days
// ------------------------------

// Go to the next day
u16DayNr = (u16DayNr + 1) % u16DaysThisYear;

// Show the events of the coming 7 days
for(uint8 u8Offset=1;u8Offset<8;u8Offset++)
{
EVENT *pEvent = g_ppEvents[u16DayNr];

// Show the events of the day (in order of importance)
while(pEvent)
{
// Is the event important enough to show?
if(pEvent->u16Importance > u8Offset-1)
{
uint8 u8Pos;

// Show the date
printf(" %2u %2u ", pEvent->u16Day, pEvent->u16Month);

// Show the importance
for(u8Pos=0;u8Pos<=pEvent->u16Importance-u8Offset;u8Pos++)
printf("*");
printf("%*s", 8 - u8Pos, "");

// Show the description
printf("%s\n", pEvent->pi8Description);
}

// Go to the next event
pEvent = pEvent->pNext;
}

// Go to the next day
u16DayNr = (u16DayNr + 1) % u16DaysThisYear;
}
}[/cpp]

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

158 WA

Post by Almost Human » Sat Aug 07, 2004 10:53 am

I've passed all input from previous threads, so I'm wondering if anyone could help me ? thanks

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Thu Nov 25, 2004 8:38 am

This is retarded! How did you figure this out? Did you get the judge's data?
I wonder why people make such stupid input for problems...
If only I had as much free time as I did in college...

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho » Wed Dec 01, 2004 6:39 pm

please...mind your language...

The specification is unclear, that's the reason I post the stuff.

I did not see the judge data, I just guessed it by intuition.
In fact, everything but (1) is well specified.

Silvia
New poster
Posts: 1
Joined: Sat Apr 15, 2006 7:10 pm
Contact:

158 - Calendar - WA

Post by Silvia » Sat Apr 15, 2006 7:18 pm

I've been trying to come up with the right code for this problem and it keeps giving me WA!!
I've searched all threads in the forum concerning this problem and, as far as I can tell, I'm doing everything right. I took into account that years wrap around themselves, I order acording to date, importance DISPLAYED, and input order... I even tried the input given in this thread: http://acm.uva.es/board/viewtopic.php?t ... hlight=158
and I got the same output as it says there!!

I'm beginning to get pretty desperate, people! Does anybody have some idea as to why it keeps giving me WA? I'm thinking it may be an error when processing the input, and I've tried several different ways, to no use. Any hints as to how I could do this?? PLEASE!

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon » Sun Apr 30, 2006 7:19 pm

Dates wrapping around year X makes no sense at all. January of year X is always before December of year X. ???

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho » Mon May 01, 2006 11:31 pm

Anniversary means it happens every year, so December of this year will lead to January of next year...

El-idioto
New poster
Posts: 45
Joined: Mon Jul 14, 2003 9:42 pm
Location: Zoetermeer, The Netherlands

Post by El-idioto » Sun May 21, 2006 10:31 am

@Silvia
I think there might be a sneaky input. I also get WA (and yes, I did try all the examples on the board).
Could someone who got AC give the examples used to test their program?

Code: Select all

#include <cstdio>

// The base types
#ifdef WIN32
  typedef __int8            int8;
  typedef __int16           int16;
  typedef __int32           int32;
  typedef __int64           int64;
  typedef unsigned __int8   uint8;
  typedef unsigned __int16  uint16;
  typedef unsigned __int32  uint32;
  typedef unsigned __int64  uint64;
#else
  typedef char                    int8;
  typedef short                   int16;
  typedef long                    int32;
  typedef long long int           int64;
  typedef unsigned char           uint8;
  typedef unsigned short          uint16;
  typedef unsigned long           uint32;
  typedef unsigned long long int  uint64;
#endif

// These macros safely delete (an array of) objects
#define SAFE_DELETE(pObj)       { if(pObj) { delete pObj;   pObj = NULL; } }
#define SAFE_DELETE_ARRAY(pArr) { if(pArr) { delete[] pArr; pArr = NULL; } }

// The specification of a calendar event
struct EVENT
{
  uint16 u16EventId;          // The unique ordinal number

  uint16 u16Day;              // The day of the month
  uint16 u16Month;            // The month
  uint16 u16Importance;       // The relative importance [1, 7]
  int8   pi8Description[256]; // The description of the event

  EVENT *pPrev;               // The previous event of the day
  EVENT *pNext;               // The next event of the day
};

// The amount of days passed after each month (for normal years and for leap years)
static uint16 DaysSoFar[2][13] =
{
  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};


// Function prototypes
void AddEvent(EVENT *pEvent);
void ShowEvents(uint16 u16Day, uint16 u16Month);

// Global variables
bool   g_bLeapYear;
EVENT *g_ppEvents[366] = {};


//---------------------------------------------------------------------------------------------
//                                      F U N C T I O N S
//---------------------------------------------------------------------------------------------


// This function is the entrance of the app
int main()
{
  uint16 u16Year;

  // Get the year
  if(scanf("%hu", &u16Year) == 1)
  {
    int8   pi8Type[2];
    uint16 u16EventId = 1;

    // Do we have a leap year?
    g_bLeapYear = !(u16Year % 4);

    // ---------------------
    //    Read the events
    // ---------------------

    // Add the next event
    while(scanf("%s", pi8Type) == 1 && pi8Type[0] == 'A')
    {
      EVENT *pEvent;
      int8   pi8Desc[256];
      uint16 u16Src = 0, u16Dst = 0;

      // Allocate a new event
      pEvent = new EVENT;
      pEvent->u16EventId = u16EventId++;

      // Get the details
      scanf("%hu %hu %hu", &pEvent->u16Day, &pEvent->u16Month, &pEvent->u16Importance);
      fgets(pi8Desc, 256, stdin);

      // Get the description
      while(pi8Desc[u16Src] == ' ')
        u16Src++;
      while(pi8Desc[u16Src] != '\0' && pi8Desc[u16Src] != '\n' && pi8Desc[u16Src] != '\r')
        pEvent->pi8Description[u16Dst++] = pi8Desc[u16Src++];
      pEvent->pi8Description[u16Dst] = '\0';

      // Add the event
      AddEvent(pEvent);
    }

    // --------------------
    //    Read the dates
    // --------------------

    // Do we have a date?
    if(pi8Type[0] == 'D')
    {
      bool   bFirst = true;
      uint16 u16Month, u16Day;

      do
      {
        // Show the events for the date
        if(scanf("%hu %hu", &u16Day, &u16Month) == 2)
        {
          // Separate the days
          if(bFirst)
            bFirst = false;
          else 
            printf("\n");

          // Show the events for the day
          ShowEvents(u16Day, u16Month);
        }
      }while(scanf("%s", pi8Type) == 1 && pi8Type[0] == 'D');
    }

    // ------------------------
    //    Free the resources
    // ------------------------

    // Free the resources
    for(uint16 u16DayNr=0;u16DayNr<366;u16DayNr++)
    {
      EVENT *pEvent = g_ppEvents[u16DayNr];

      // Delete all the events of the day
      while(pEvent)
      {
        EVENT *pEventNext = pEvent->pNext;

        // Delete the event
        SAFE_DELETE(pEvent);
        pEvent = pEventNext;
      }
    }
  }

  return 0;
}


//---------------------------------------------------------------------------------------------


// This function adds an event to the calendar
void AddEvent(EVENT *pNewEvent)
{
  uint16 u16DayNr;

  // Get the event's day number
  u16DayNr = uint16(DaysSoFar[g_bLeapYear][pNewEvent->u16Month-1] + pNewEvent->u16Day - 1);

  // Don't we have any events yet?
  if(g_ppEvents[u16DayNr] == NULL)
  {
    // Set the first event of the day
    pNewEvent->pPrev = NULL;
    pNewEvent->pNext = NULL;
    g_ppEvents[u16DayNr] = pNewEvent;
  }
  else
  {
    EVENT *pEventTemp = g_ppEvents[u16DayNr];

    // Don't we have a more important event than the most important event of the day?
    if(pNewEvent->u16Importance <= pEventTemp->u16Importance)
    {
      // Find the insertion point
      while(pEventTemp->pNext && pEventTemp->pNext->u16Importance >= pNewEvent->u16Importance)
        pEventTemp = pEventTemp->pNext;
    }

    // Should we insert the new event before the existing event?
    if(pNewEvent->u16Importance > pEventTemp->u16Importance)
    {
      // Insert the new event
      if(pNewEvent->pPrev = pEventTemp->pPrev)
        pNewEvent->pPrev->pNext = pNewEvent;
      pNewEvent->pNext  = pEventTemp;
      pEventTemp->pPrev = pNewEvent;
    }
    else
    {
      // Our event is the least important event of the day
      pNewEvent->pPrev  = pEventTemp;
      pNewEvent->pNext  = NULL;
      pEventTemp->pNext = pNewEvent;
    }

    // Make sure we're still pointing to the first event
    while(g_ppEvents[u16DayNr]->pPrev)
      g_ppEvents[u16DayNr] = g_ppEvents[u16DayNr]->pPrev;
  }
}


//---------------------------------------------------------------------------------------------


// This function shows the events in the coming days
void ShowEvents(uint16 u16Day, uint16 u16Month)
{
  uint16 u16DaysThisYear = (g_bLeapYear ? 366 : 365), u16DayNr;

  // Show which day it is today
  printf("Today is: %2u %2u\n", u16Day, u16Month);

  // --------------------
  //    Today's events
  // --------------------

  // Get the day nr
  u16DayNr = DaysSoFar[g_bLeapYear][u16Month-1] + u16Day - 1;

  // Do we have any events today?
  if(g_ppEvents[u16DayNr])
  {
    uint16 u16MinimumId = 0;

    // Show today's events in order of input
    for(;;)
    {
      EVENT *pCurEvent = g_ppEvents[u16DayNr], *pEvent = NULL;
      uint16 u16LowestId = 65535;

      // Check the events
      while(pCurEvent)
      {
        // Do we have a new minimum event (but higher than the previous one)?
        if(pCurEvent->u16EventId < u16LowestId && pCurEvent->u16EventId > u16MinimumId)
        {
          // We have the lowest so far
          pEvent = pCurEvent;
          u16LowestId = pEvent->u16EventId;
        }

        // Go to the next event
        pCurEvent = pCurEvent->pNext;
      }

      // Do we have a lowest event?
      if(pEvent)
      {
        // Show the event
        printf(" %2u %2u *TODAY* %s\n", pEvent->u16Day, pEvent->u16Month, pEvent->pi8Description);
        u16MinimumId = pEvent->u16EventId;
      }
      else
        break;
    }
  }

  // ------------------------------
  //    Events for coming 7 days
  // ------------------------------

  // Go to the next day
  u16DayNr = (u16DayNr + 1) % u16DaysThisYear;

  // Show the events of the coming 7 days
  for(uint8 u8Offset=1;u8Offset<8;u8Offset++)
  {
    EVENT *pEvent = g_ppEvents[u16DayNr];

    // Show the events of the day (in order of importance)
    while(pEvent)
    {
      // Is the event important enough to show?
      if(pEvent->u16Importance > u8Offset-1)
      {
        uint8 u8Pos;

        // Show the date
        printf(" %2u %2u ", pEvent->u16Day, pEvent->u16Month);

        // Show the importance
        for(u8Pos=0;u8Pos<=pEvent->u16Importance-u8Offset;u8Pos++)
          printf("*");
        printf("%*s", 8 - u8Pos, "");

        // Show the description
        printf("%s\n", pEvent->pi8Description);
      }

      // Go to the next event
      pEvent = pEvent->pNext;
    }

    // Go to the next day
    u16DayNr = (u16DayNr + 1) % u16DaysThisYear;
  }
}

LithiumDex
New poster
Posts: 44
Joined: Tue Jun 06, 2006 6:44 pm
Location: Nova Scotia, Canada
Contact:

158... poor wording?

Post by LithiumDex » Fri Aug 18, 2006 6:03 pm

In the problem statement, it says: "If several events are scheduled for the same day, order them by relative importance (number of stars)."

And unless I'm taking this very wrong, it means order by DATE first, and then number of stars... yet I didn't get AC until i order it the other way around.. Can anyone explain it?
- Chris Adams

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon » Sun Aug 27, 2006 2:15 am

Agreed, this problem is a guessing game. And there are many more problems like this in the archive.

Post Reply

Return to “Volume 1 (100-199)”