Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For a fixed budget , the customer does not pick the cheapest sofa. They pick the first sofa in the list whose price is at most .
Think about what must be true before sofa in the list. If some earlier sofa is affordable, the customer never reaches sofa .
Sofa can be ordered only if there exists a budget such that , but every earlier price is greater than .
That means we need . Such an exists exactly when is smaller than every price before it.
So the orderable sofas are exactly the left-to-right prefix minima. Every sofa that is not a new minimum price is doomed. Furniture store? More like prefix-min detector.
A customer scans sofas from left to right and buys the first sofa with price at most their budget .
That word first is the whole problem. A sofa can be cheap enough and still never get bought, because an earlier affordable sofa blocks it.
When can sofa be bought?
Suppose a customer buys sofa .
Two things must be true:
The second condition means:
So for sofa , we need some budget satisfying:
Such a budget exists exactly when:
In plain English: sofa must be cheaper than every sofa before it.
So the sofas that can be ordered are precisely the new minimum prices while scanning from left to right.
The first sofa is always orderable, because there is nothing before it. Just pick any budget .
For every later sofa:
Algorithm
Maintain the minimum price seen so far, call it mn.
Scan sofas from left to right:
mn = a_i.At the end, print all stored indices.
The indices are naturally collected in increasing order because we scan from left to right.
Complexity
For each test case, we do one scan:
Memory usage is for the answer list. With , this is extremely chill.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> bad;
int mn = INT_MAX;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x < mn) {
mn = x;
} else {
bad.push_back(i);
}
}
cout << bad.size() << '\n';
for (int i = 0; i < (int)bad.size(); i++) {
if (i) cout << ' ';
cout << bad[i];
}
cout << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> bad;
int mn = INT_MAX;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x < mn) {
mn = x;
} else {
bad.push_back(i);
}
}
cout << bad.size() << '\n';
for (int i = 0; i < (int)bad.size(); i++) {
if (i) cout << ' ';
cout << bad[i];
}
cout << '\n';
}
}