fix: main ac
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,23 @@
|
||||
\documentclass{maratona}
|
||||
\usepackage[boxed,algoruled,linesnumbered,vlined,longend]{algorithm2e}
|
||||
\SetKwFor{For}{for $($}{$)$}{}
|
||||
\SetKwFor{ForAll}{for all $($}{$)$}{}
|
||||
\SetKwFor{If}{if $($}{$)$}{}
|
||||
\SetKwFor{ElseIf}{else if $($}{$)$}{}
|
||||
\newcommand{\Call}[2]{\textsc{#1}(#2)}
|
||||
\SetFuncSty{textsc}
|
||||
\DontPrintSemicolon
|
||||
|
||||
\newenvironment{algoritmo}[1][htbp]
|
||||
{
|
||||
\begingroup
|
||||
\renewcommand{\algorithmcfname}{Algoritmo}
|
||||
\begin{algorithm}[#1]
|
||||
}
|
||||
{
|
||||
\end{algorithm}
|
||||
\endgroup
|
||||
}
|
||||
\begin{document}
|
||||
\begin{ProblemaAutor}{}{Inclusão de Subintervalos}{1}{256}{}
|
||||
|
||||
@@ -22,6 +40,7 @@ As próximas \( n \) linhas contêm, cada uma, dois inteiros \( l_i \) e \( r_i
|
||||
Para cada caso de teste, imprima um único inteiro representando o tamanho do menor conjunto \( S' \subseteq S \) que cobre todos os intervalos de \( S \).
|
||||
|
||||
\ExemploEntrada
|
||||
|
||||
\begin{Exemplo}
|
||||
\texttt{3} & \texttt{3}\\
|
||||
\texttt{1~2} & \\
|
||||
|
||||
@@ -1 +1 @@
|
||||
3994
|
||||
3999
|
||||
|
||||
@@ -1 +1 @@
|
||||
2173
|
||||
2174
|
||||
|
||||
@@ -1 +1 @@
|
||||
12766
|
||||
12937
|
||||
|
||||
@@ -1 +1 @@
|
||||
7847
|
||||
7881
|
||||
|
||||
@@ -1 +1 @@
|
||||
10267
|
||||
10345
|
||||
|
||||
@@ -1 +1 @@
|
||||
28531
|
||||
36399
|
||||
|
||||
@@ -1 +1 @@
|
||||
27668
|
||||
32619
|
||||
|
||||
@@ -1 +1 @@
|
||||
27428
|
||||
32151
|
||||
|
||||
@@ -1 +1 @@
|
||||
27749
|
||||
33228
|
||||
|
||||
@@ -1 +1 @@
|
||||
4267
|
||||
4270
|
||||
|
||||
@@ -1 +1 @@
|
||||
28687
|
||||
38406
|
||||
|
||||
@@ -1 +1 @@
|
||||
28688
|
||||
37218
|
||||
|
||||
@@ -1 +1 @@
|
||||
28408
|
||||
35870
|
||||
|
||||
@@ -1 +1 @@
|
||||
13893
|
||||
14128
|
||||
|
||||
@@ -1 +1 @@
|
||||
28305
|
||||
36385
|
||||
|
||||
@@ -1 +1 @@
|
||||
2
|
||||
3
|
||||
|
||||
@@ -1 +1 @@
|
||||
23348
|
||||
25202
|
||||
|
||||
@@ -1 +1 @@
|
||||
27297
|
||||
32097
|
||||
|
||||
@@ -1 +1 @@
|
||||
25660
|
||||
28767
|
||||
|
||||
@@ -1 +1 @@
|
||||
20444
|
||||
21477
|
||||
|
||||
@@ -1 +1 @@
|
||||
28224
|
||||
34785
|
||||
|
||||
@@ -1 +1 @@
|
||||
3
|
||||
4
|
||||
|
||||
@@ -1 +1 @@
|
||||
3266
|
||||
3268
|
||||
|
||||
@@ -1 +1 @@
|
||||
3378
|
||||
3381
|
||||
|
||||
@@ -1 +1 @@
|
||||
2509
|
||||
2511
|
||||
|
||||
@@ -1 +1 @@
|
||||
4084
|
||||
4092
|
||||
|
||||
@@ -43,8 +43,8 @@
|
||||
},
|
||||
"solutions": {
|
||||
"main-ac": "ac.cpp",
|
||||
"alternative-ac": [],
|
||||
"wrong-answer": ["WA.cpp"],
|
||||
"alternative-ac": ["caio.cpp"],
|
||||
"wrong-answer": ["wa.cpp"],
|
||||
"time-limit": ["TLE.cpp"],
|
||||
"time-limit-or-ac": [],
|
||||
"time-limit-or-memory-limit": [],
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool comp(pair<int, int> &a, pair<int, int> &b) {
|
||||
if (a.first != b.first) {
|
||||
return a.first < b.first;
|
||||
}
|
||||
return a.second > b.second;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
int n; cin >> n;
|
||||
|
||||
vector<pair<int, int>> intervals(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> intervals[i].first;
|
||||
cin >> intervals[i].second;
|
||||
}
|
||||
|
||||
sort(intervals.begin(), intervals.end(), comp);
|
||||
|
||||
int lastCovered = intervals[0].second;
|
||||
int count = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (lastCovered < intervals[i].second) {
|
||||
lastCovered = intervals[i].second;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -9,9 +9,6 @@ bool comp(pair<int, int> &a, pair<int, int> &b) {
|
||||
return a.second > b.second;
|
||||
}
|
||||
|
||||
bool intersecting(pair<int, int> &a, pair<int, int> &b) {
|
||||
return a.second >= b.first;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n; cin >> n;
|
||||
@@ -24,40 +21,13 @@ int main(){
|
||||
|
||||
sort(intervals.begin(), intervals.end(), comp);
|
||||
|
||||
// first sort by start time, if two intervals start at the same time the one with the highest end time comes first
|
||||
// always include the first interval
|
||||
// keep track of the last included interval so far
|
||||
// while there are intervals intersecting with the last included interval take the one with the highest ending value
|
||||
//
|
||||
// if there is some intersecting interval check if its end time is greater than the last included interval end time, if so include it and update count, else repeat process from the last intersecting interval
|
||||
// if no intervals intersect with the last included interval then just process the next interval in order
|
||||
|
||||
// in both cases update the count by 1
|
||||
|
||||
int curInterval = 1, lastIncludedInterval = 0, count = 1;
|
||||
while (curInterval < n) { // curInterval holds the first interval thaat was not processed yet
|
||||
int bestIntersectingInterval = lastIncludedInterval, j = curInterval;
|
||||
while (j < n && intersecting(intervals[lastIncludedInterval], intervals[j])) {
|
||||
int bestEndTime = intervals[bestIntersectingInterval].second;
|
||||
int endTime = intervals[j].second;
|
||||
if (endTime > bestEndTime) {
|
||||
bestIntersectingInterval = j;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
|
||||
if (bestIntersectingInterval != lastIncludedInterval) {
|
||||
lastIncludedInterval = bestIntersectingInterval;
|
||||
count++;
|
||||
} else {
|
||||
if (j == n) break; // this interval alredy covers every remaining interval
|
||||
|
||||
lastIncludedInterval = j;
|
||||
int lastCovered = intervals[0].second;
|
||||
int count = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (lastCovered < intervals[i].second) {
|
||||
lastCovered = intervals[i].second;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (curInterval == j) curInterval++;
|
||||
else curInterval = j;
|
||||
}
|
||||
|
||||
cout << count << endl;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
\usepackage[boxed,algoruled,linesnumbered,vlined,longend]{algorithm2e}
|
||||
\SetKwFor{For}{for $($}{$)$}{}
|
||||
\SetKwFor{ForAll}{for all $($}{$)$}{}
|
||||
\SetKwFor{If}{if $($}{$)$}{}
|
||||
\SetKwFor{ElseIf}{else if $($}{$)$}{}
|
||||
\newcommand{\Call}[2]{\textsc{#1}(#2)}
|
||||
\SetFuncSty{textsc}
|
||||
\DontPrintSemicolon
|
||||
|
||||
\newenvironment{algoritmo}[1][htbp]
|
||||
{
|
||||
\begingroup
|
||||
\renewcommand{\algorithmcfname}{Algoritmo}
|
||||
\begin{algorithm}[#1]
|
||||
}
|
||||
{
|
||||
\end{algorithm}
|
||||
\endgroup
|
||||
}
|
||||
Reference in New Issue
Block a user