#include <vector> #include <math.h> #include <string> #include <iostream> #include <sstream> #include <limits> #include <cstring> #include <array> #include <algorithm> //#define PA2016_TEST #ifdef PA2016_TEST #define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); } #define PAUSE_CONSOLE system( "pause" ); #define DEBUG_ONLY( line ) line; #else #define ASSERT( condition ) #define PAUSE_CONSOLE #define DEBUG_ONLY( line ) #endif typedef unsigned int uint; int Min( int a, int b ) { return a < b ? a : b; } struct STaskInfo { STaskInfo( uint startTime, uint endTime, uint parts ) : m_startTime( startTime ) , m_endTime( endTime ) , m_parts( parts ) {} uint m_startTime; uint m_endTime; uint m_parts; }; struct SPriority { SPriority( double priority, STaskInfo* task ) : m_priority( priority ) , m_task( task ) { } double m_priority; STaskInfo* m_task; }; struct STimelineEvent { STimelineEvent( uint time, STaskInfo* info, bool isEnd ) : m_time( time ) , m_task( info ) , m_isEnd( isEnd ) { } STaskInfo* m_task; uint m_time; bool m_isEnd; }; class Scheduler { public: Scheduler( uint numOfTasks, uint processors ) : m_processors( processors ) { m_taskInfos.reserve( numOfTasks ); m_openTasks.reserve( numOfTasks ); m_tempPriorities.reserve( numOfTasks ); } void AddTask( uint startTime, uint endTime, uint parts ) { m_taskInfos.push_back( STaskInfo( startTime, endTime, parts ) ); STaskInfo* info = &m_taskInfos[ m_taskInfos.size() - 1 ]; AddEvent( info, startTime, false ); AddEvent( info, endTime - 1, true ); } bool TryToSchedule() { // Sort timeline: std::sort( m_timeline.begin(), m_timeline.end(), []( const STimelineEvent& a, const STimelineEvent& b ) { return a.m_time < b.m_time; } ); uint prevTime = m_timeline[ 0 ].m_time - 1; const uint timelineSize = m_timeline.size(); for( uint i = 0; i < timelineSize; ++i ) { // Process events( add to open task list if needed ): uint curr_time = m_timeline[ i ].m_time; OnEvent( m_timeline[ i ] ); bool cont = true; for( uint j = i + 1; j < timelineSize; ++j ) { if( m_timeline[ j ].m_time <= curr_time ) { OnEvent( m_timeline[ j ] ); } else { // We have processed more events, so update outher loop. i = j - 1; break; } } if( !Schedule( curr_time - prevTime, curr_time ) ) return false; prevTime = curr_time; } return true; } private: bool Schedule( uint timeSlice, uint currTime ) { m_tempPriorities.clear(); // Calculate priority queue: for( auto* task : m_openTasks ) { int priority = CalcPriority( task, currTime, timeSlice ); if( priority < 0 ) return false; // Scheduler has failed to schedule task at time. m_tempPriorities.push_back( SPriority( priority, task ) ); } // Sort priorites: std::sort( m_tempPriorities.begin(), m_tempPriorities.end(), []( const SPriority& a, const SPriority& b ) { return a.m_priority < b.m_priority; } ); GrabTasks( timeSlice ); RemoveDoneTasksFromOpenList(); return true; } void GrabTasks( uint timeslicePerSingleProc ) { int slotsAvailalbeAcrossAllProcessors = timeslicePerSingleProc*m_processors; for( auto priority : m_tempPriorities ) { uint& taskParts = priority.m_task->m_parts; int partsTaken = Min( taskParts, timeslicePerSingleProc ); taskParts -= partsTaken; slotsAvailalbeAcrossAllProcessors -= partsTaken; if( slotsAvailalbeAcrossAllProcessors <= 0 ) { // We have reach maximum workload. It is possible that some tasks part cannot // be taken, so return them: taskParts += -slotsAvailalbeAcrossAllProcessors; return; } } } void RemoveDoneTasksFromOpenList() { uint openTasksSize = m_openTasks.size(); for( uint i = 0; i < openTasksSize; ) { if( m_openTasks[ i ]->m_parts == 0 ) { m_openTasks[ i ] = m_openTasks[ openTasksSize - 1 ]; --openTasksSize; } else ++i; } m_openTasks.resize( openTasksSize ); } void AddEvent( STaskInfo* task, uint time, bool isEnd ) { m_timeline.push_back( STimelineEvent( time, task, isEnd ) ); } void OnEvent( const STimelineEvent& event ) { if( !event.m_isEnd ) m_openTasks.push_back( event.m_task ); } int CalcPriority( const STaskInfo* info, uint currTime, uint timeslice ) { // 0 - biggest priorty; auto val = info->m_endTime - info->m_parts - currTime + timeslice - 1; return val; } std::vector< STaskInfo > m_taskInfos; std::vector< STimelineEvent > m_timeline; std::vector< STaskInfo* > m_openTasks; std::vector< SPriority > m_tempPriorities; uint m_processors; }; void TestAll() { } int main() { DEBUG_ONLY( TestAll() ); uint numOfTasks = 0; std::cin >> numOfTasks; uint numOfProcessors = 0; std::cin >> numOfProcessors; Scheduler scheduler( numOfTasks, numOfProcessors ); for( uint i = 0; i < numOfTasks; ++i ) { uint taskStart = 0; std::cin >> taskStart; uint taskMaxEnd = 0; std::cin >> taskMaxEnd; uint taskParts = 0; std::cin >> taskParts; scheduler.AddTask( taskStart, taskMaxEnd, taskParts ); } if( scheduler.TryToSchedule() ) std::cout << "TAK"; else std::cout << "NIE"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | #include <vector> #include <math.h> #include <string> #include <iostream> #include <sstream> #include <limits> #include <cstring> #include <array> #include <algorithm> //#define PA2016_TEST #ifdef PA2016_TEST #define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); } #define PAUSE_CONSOLE system( "pause" ); #define DEBUG_ONLY( line ) line; #else #define ASSERT( condition ) #define PAUSE_CONSOLE #define DEBUG_ONLY( line ) #endif typedef unsigned int uint; int Min( int a, int b ) { return a < b ? a : b; } struct STaskInfo { STaskInfo( uint startTime, uint endTime, uint parts ) : m_startTime( startTime ) , m_endTime( endTime ) , m_parts( parts ) {} uint m_startTime; uint m_endTime; uint m_parts; }; struct SPriority { SPriority( double priority, STaskInfo* task ) : m_priority( priority ) , m_task( task ) { } double m_priority; STaskInfo* m_task; }; struct STimelineEvent { STimelineEvent( uint time, STaskInfo* info, bool isEnd ) : m_time( time ) , m_task( info ) , m_isEnd( isEnd ) { } STaskInfo* m_task; uint m_time; bool m_isEnd; }; class Scheduler { public: Scheduler( uint numOfTasks, uint processors ) : m_processors( processors ) { m_taskInfos.reserve( numOfTasks ); m_openTasks.reserve( numOfTasks ); m_tempPriorities.reserve( numOfTasks ); } void AddTask( uint startTime, uint endTime, uint parts ) { m_taskInfos.push_back( STaskInfo( startTime, endTime, parts ) ); STaskInfo* info = &m_taskInfos[ m_taskInfos.size() - 1 ]; AddEvent( info, startTime, false ); AddEvent( info, endTime - 1, true ); } bool TryToSchedule() { // Sort timeline: std::sort( m_timeline.begin(), m_timeline.end(), []( const STimelineEvent& a, const STimelineEvent& b ) { return a.m_time < b.m_time; } ); uint prevTime = m_timeline[ 0 ].m_time - 1; const uint timelineSize = m_timeline.size(); for( uint i = 0; i < timelineSize; ++i ) { // Process events( add to open task list if needed ): uint curr_time = m_timeline[ i ].m_time; OnEvent( m_timeline[ i ] ); bool cont = true; for( uint j = i + 1; j < timelineSize; ++j ) { if( m_timeline[ j ].m_time <= curr_time ) { OnEvent( m_timeline[ j ] ); } else { // We have processed more events, so update outher loop. i = j - 1; break; } } if( !Schedule( curr_time - prevTime, curr_time ) ) return false; prevTime = curr_time; } return true; } private: bool Schedule( uint timeSlice, uint currTime ) { m_tempPriorities.clear(); // Calculate priority queue: for( auto* task : m_openTasks ) { int priority = CalcPriority( task, currTime, timeSlice ); if( priority < 0 ) return false; // Scheduler has failed to schedule task at time. m_tempPriorities.push_back( SPriority( priority, task ) ); } // Sort priorites: std::sort( m_tempPriorities.begin(), m_tempPriorities.end(), []( const SPriority& a, const SPriority& b ) { return a.m_priority < b.m_priority; } ); GrabTasks( timeSlice ); RemoveDoneTasksFromOpenList(); return true; } void GrabTasks( uint timeslicePerSingleProc ) { int slotsAvailalbeAcrossAllProcessors = timeslicePerSingleProc*m_processors; for( auto priority : m_tempPriorities ) { uint& taskParts = priority.m_task->m_parts; int partsTaken = Min( taskParts, timeslicePerSingleProc ); taskParts -= partsTaken; slotsAvailalbeAcrossAllProcessors -= partsTaken; if( slotsAvailalbeAcrossAllProcessors <= 0 ) { // We have reach maximum workload. It is possible that some tasks part cannot // be taken, so return them: taskParts += -slotsAvailalbeAcrossAllProcessors; return; } } } void RemoveDoneTasksFromOpenList() { uint openTasksSize = m_openTasks.size(); for( uint i = 0; i < openTasksSize; ) { if( m_openTasks[ i ]->m_parts == 0 ) { m_openTasks[ i ] = m_openTasks[ openTasksSize - 1 ]; --openTasksSize; } else ++i; } m_openTasks.resize( openTasksSize ); } void AddEvent( STaskInfo* task, uint time, bool isEnd ) { m_timeline.push_back( STimelineEvent( time, task, isEnd ) ); } void OnEvent( const STimelineEvent& event ) { if( !event.m_isEnd ) m_openTasks.push_back( event.m_task ); } int CalcPriority( const STaskInfo* info, uint currTime, uint timeslice ) { // 0 - biggest priorty; auto val = info->m_endTime - info->m_parts - currTime + timeslice - 1; return val; } std::vector< STaskInfo > m_taskInfos; std::vector< STimelineEvent > m_timeline; std::vector< STaskInfo* > m_openTasks; std::vector< SPriority > m_tempPriorities; uint m_processors; }; void TestAll() { } int main() { DEBUG_ONLY( TestAll() ); uint numOfTasks = 0; std::cin >> numOfTasks; uint numOfProcessors = 0; std::cin >> numOfProcessors; Scheduler scheduler( numOfTasks, numOfProcessors ); for( uint i = 0; i < numOfTasks; ++i ) { uint taskStart = 0; std::cin >> taskStart; uint taskMaxEnd = 0; std::cin >> taskMaxEnd; uint taskParts = 0; std::cin >> taskParts; scheduler.AddTask( taskStart, taskMaxEnd, taskParts ); } if( scheduler.TryToSchedule() ) std::cout << "TAK"; else std::cout << "NIE"; return 0; } |