Validate a Mutex Ownership Trace
Find invalid lock and unlock operations in a sequentially recorded trace.
A debugger records mutex operations in a single chronological trace. Each event is either L thread_id (lock) or U thread_id (unlock). The mutex is initially unlocked. A lock is valid only when it is unlocked; an unlock is valid only when the requesting thread owns it. Invalid operations increment an error counter and leave mutex state unchanged. Count errors. This is a trace-validation exercise, not a substitute for true concurrent race detection.
Input format
First line: N. Next N lines: L id or U id.
Output format
Print the number of invalid operations.
Examples
6 L 1 L 2 U 2 U 1 L 2 U 2
2
Thread 2 cannot lock or unlock the mutex while thread 1 owns it.
Constraints
- 0 ≤ N ≤ 100000
- 1 ≤ id ≤ 10^9
Related concepts
This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.
Loading Monaco C++ editor…
Checking whether the external C++ judge is available.
6 L 1 L 2 U 2 U 1 L 2 U 2
2