Vibe code scripts to edit and add usaco solutions

This commit is contained in:
2026-08-25 00:45:15 +01:00
parent a79ff12290
commit 6fd6d70b04
26 changed files with 1156 additions and 6 deletions
+4
View File
@@ -1 +1,5 @@
*.out
*.bak
solving/*/main
solved/*/main
skipped/*/main
+83
View File
@@ -0,0 +1,83 @@
# USACO Workspace
Practice workspace for USACO problems, managed by the `./usaco` script.
## Layout
```
template.cpp starting template for every problem
solving/<name>/ problems in progress
solved/<name>/ finished problems
skipped/<name>/ problems set aside for later
```
Each problem dir contains `main.cpp` plus `<io>.in` / `<io>.out` files (USACO file IO is wired up automatically via `STDIO("<io>")`).
## Commands
### Start a problem
```
./usaco new diamond-collector
./usaco new diamond-collector diamond # if the freopen name differs
./usaco new diamond-collector --url https://usaco.org/index.php?page=viewproblem2&cpid=639
```
Creates `solving/<name>/main.cpp` from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
**Prefix matching:** everywhere a command takes a problem name, a unique prefix works — `./usaco e diam` finds `diamond-collector` (ambiguous prefixes list the candidates).
### Build & run
```
./usaco run diamond-collector # compile + run, then print the .out file
./usaco build diamond-collector # compile only
```
Compiles with `g++ -O2 -std=c++17 -Wall -Wextra` and runs inside the problem dir so file IO just works. Put your test input in the `.in` file first.
### Finish (or give up on) a problem
```
./usaco done diamond-collector # move to solved/ and auto git commit "Solve <name>"
./usaco skip diamond-collector # move to skipped/
./usaco undo diamond-collector # bring back to solving/ from solved/ or skipped/
```
### Edit
```
./usaco vim diamond-collector # open main.cpp in $EDITOR (defaults to vim)
./usaco e diamond-collector # same thing, shorter (also: edit)
```
Opens from inside the problem dir, so the `.in`/`.out` files are right there.
### Look around
```
./usaco list # all problems by status, oldest-touched first
./usaco stats # counts + problems solved in the last 7 / 30 days
./usaco open diamond-collector # open the saved problem URL in a browser
```
### Misc
```
./usaco template diamond-collector # reset main.cpp from template (old one saved as main.cpp.bak)
./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere
./usaco path diamond-collector # print the problem's directory
```
## Shell integration (recommended)
Add to `~/.zshrc`:
```
source ~/Coding/USACO/usaco.zsh
```
This gives you:
- `usaco` as a global command (no `./` needed, works from any directory)
- tab completion for commands and problem names (`usaco e diam<TAB>`)
- `ucd <name>` — cd straight into a problem's directory
## Typical session
```
./usaco new the-lost-cow --url <link>
./usaco open the-lost-cow # read the problem
# paste sample input into solving/the-lost-cow/the-lost-cow.in
# edit solving/the-lost-cow/main.cpp
./usaco run the-lost-cow # iterate until the output is right
./usaco done the-lost-cow # 🎉 (auto-commits)
```
+7
View File
@@ -0,0 +1,7 @@
3 8
AATCCCAT
GATTGCAA
GGTCGCAA
ACTCCCAG
ACTCGCAT
ACTTCCAT
+70
View File
@@ -0,0 +1,70 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("cownomics.in");
STDOUT("cownomics.out");
takeII(n, m);
vector<set<char>> S(m, set<char>());
vector<set<char>> P(m, set<char>());
F(i, 0, n) {
F(j, 0, m) {
char k;
cin >> k;
S[j].insert(k);
}
}
F(i, 0, n) {
F(j, 0, m) {
char k;
cin >> k;
P[j].insert(k);
}
}
int res = 0;
F(j, 0, m) {
vector<char> intersect;
set_intersection(S[j].begin(), S[j].end(), P[j].begin(), P[j].end(), back_inserter(intersect));
if (intersect.size() == 0) {
res++;
}
}
cout << res;
}
+4
View File
@@ -0,0 +1,4 @@
3 4
4 1 2 3
4 1 3 2
4 2 1 3
+72
View File
@@ -0,0 +1,72 @@
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("gymnastics.in");
STDOUT("gymnastics.out");
takeII(k, n);
vector<vector<int>> orders(k, vector<int>(n));
vector<vector<int>> orders2(k, vector<int>(n));
F(i,0,k) {
F(j,0,n) {
takeI(u);
u--;
orders[i][j] = u;
orders2[i][u] = j;
}
}
set<pair<int, int>> pairs;
F(i,0,n-1) {
F(j, i+1, n) {
pairs.insert({orders[0][i], orders[0][j]});
}
}
F(i,1,k) {
for (auto it = pairs.begin(); it != pairs.end(); ) {
auto [l, r] = *it;
if (orders2[i][l] > orders2[i][r])
it = pairs.erase(it); // erase returns the next valid iterator
else
++it;
}
if (pairs.empty()) break; // nothing left to filter
}
cout << pairs.size();
}
+8
View File
@@ -0,0 +1,8 @@
7 10
7 3
5 5
9 7
3 1
7 7
5 3
9 1
+84
View File
@@ -0,0 +1,84 @@
#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIO("balancing");
takeII(n, k);
vector<pair<int, int>> coords(n);
F(i,0,n) {
int x, y;
cin >> x >> y;
coords[i] = {x,y};
}
// Sort by X to easily separate below a and above a
int M = INT_MAX;
F(i,0,n) {
int a = coords[i].first+1;
F(j,0,n) {
int b = coords[j].second+1;
int first, second, third, fourth;
first = second = third = fourth = 0;
F(k,0,n) {
// Separate X by a
if (coords[k].first < a) {
// Separate Y by b
if (coords[k].second < b) {
first++;
} else {
second++;
}
} else {
// Separate Y by b
if (coords[k].second < b) {
third++;
} else {
fourth++;
}
}
}
int maxxx = max(max(max(first, second), third), fourth);
M = REMIN(M, maxxx);
}
}
cout << M;
}
+49
View File
@@ -0,0 +1,49 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("pails.in");
STDOUT("pails.out");
takeIII(x,y,m);
int currX = 0, res = 0;
while (currX <= m) {
int currY = y * ((m - currX) / y);
res = max(res, currX + currY);
currX += x;
}
cout << res;
}
+1
View File
@@ -0,0 +1 @@
17 25 77
+57
View File
@@ -0,0 +1,57 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("sleepy.in");
STDOUT("sleepy.out");
takeI(n);
vector<int> order(n);
F(i,0,n) {
int ord;
cin >> ord;
order[i] = ord-1;
}
int startOfIncreasingSequence = 0;
FDE(i,n-1,1) {
if (order[i-1] >= order[i]) {
startOfIncreasingSequence = i;
break;
}
}
cout << startOfIncreasingSequence;
}
+3
View File
@@ -0,0 +1,3 @@
4
1 2 4 3
+73
View File
@@ -0,0 +1,73 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("taming.in");
STDOUT("taming.out");
takeI(n);
vector<int> logs(n);
F(i,0,n) cin >> logs[i];
if (logs[0] > 0) {
cout << -1 << endl;
return 0;
}
logs[0] = 0;
int countdown = -1, min_breakouts = 0, max_breakouts = 0;
FDE(i,n-1,0) {
if (countdown >= 0 and logs[i] >= 0 and logs[i] != countdown) {
cout << -1 << endl;
return 0;
}
if (logs[i] >= 0) {
countdown = logs[i];
}
if (countdown <= 0) {
max_breakouts++;
}
if (countdown == 0) {
min_breakouts++;
}
countdown--;
}
cout << min_breakouts << ' ' << max_breakouts << endl;
}
+2
View File
@@ -0,0 +1,2 @@
4
-1 -1 -1 1
@@ -0,0 +1 @@
ABCCABDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ
@@ -0,0 +1,68 @@
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
// ----- VECTOR -----
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("circlecross.in");
STDOUT("circlecross.out");
vector<pair<int,int>> C(26, pair<int,int>(-1, -1) );
F(i,0,52) {
char ch;
cin >> ch;
size_t idx = ch - 'A';
if (C[idx].first == -1) {
C[idx].first = i;
} else {
C[idx].second = i;
}
}
int res = 0;
F(i,0,25) {
F(j,i+1,26) {
auto a = C[i];
auto b = C[j];
if (b.first < a.first or b.first > a.second) {
if ( b.second > a.first and b.second < a.second) res++;
} else {
if ( b.second < a.first or b.second > a.second) res++;
}
}
}
cout << res;
}
+1
View File
@@ -0,0 +1 @@
https://usaco.org/index.php?page=viewproblem2&cpid=1469
+85
View File
@@ -0,0 +1,85 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
int main() {
FASTIO;
takeI(n);
takeV(a,n);
takeV(b,n);
vi sol(n+1,0);
F(l,0,n) {
F(r,l,n) {
reverse(a.begin() + l, a.begin() + r + 1);
int correct = 0;
F(i,0,n) {
if (b[i] == a[i]) correct++;
}
sol[correct]++;
}
}
for (auto i : sol) {
cout << i << NL;
}
}
+9
View File
@@ -0,0 +1,9 @@
7 3
10
5
1
12
9
5
14
+69
View File
@@ -0,0 +1,69 @@
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
STDIN("diamond.in");
STDOUT("diamond.out");
takeII(n,k);
vector<int> s(n+1), b1(n), b2(n);
s[n] = INT_MAX;
F(i,0,n) cin >> s[i];
sort(ALL(s));
int l = 0, big = 0;
F(r,0,n) {
while (s[r] - s[l] >= k+1) l++;
REMAX(big, r-l+1);
b1[r] = big;
}
int r = n-1;
big = 0;
FD(l,n-1,-1) {
while (s[r] - s[l] >= k+1) r--;
REMAX(big, r-l+1);
b2[l] = big;
}
int sol = 0;
F(i,1,n) {
REMAX(sol, b1[i-1] + b2[i]);
}
cout << sol;
}
+71
View File
@@ -0,0 +1,71 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define NL '\n'
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
takeI(n);
vector<int> weights(2*n);
F(i,0,2*n) cin >> weights[i];
sort(ALL(weights));
vector<int> distances(2*n-1);
F(i,0,2*n-1) distances[i] = abs(weights[i] - weights[i+1]);
int res = 0;
F(i,0,n-1) {
auto it = min_element(ALL(distances));
size_t idx = distance(distances.begin(), it);
res += *it;
if (it == distances.begin()) {
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
distances.erase(it, it+2);
} else if (it == distances.end() - 1) {
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
distances.erase(it-1, it+1);
} else {
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
distances.erase(it, it+2);
distances[idx-1] = abs(weights[idx-1] - weights[idx]);
}
// find i st distances[i] is minimal
// remove distances[i], weights[i]
// recompute distances[i-1], distances[i]
}
cout << res;
}
+48
View File
@@ -0,0 +1,48 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
int main() {
FASTIO;
whileZ {
takeI(n);
vector<int> a;
F(i,0,n) cin >> a[i];
}
}
+31 -6
View File
@@ -4,10 +4,18 @@
#include <vector>
using namespace std;
// ----- FILE IO -----
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
@@ -16,22 +24,39 @@ using namespace std;
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int (n); cin >> n
#define takeII(n,m) int (n),(m); cin >> n >> m
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
int main() {
takeI(n);
Executable
+201
View File
@@ -0,0 +1,201 @@
#!/usr/bin/env bash
# usaco - manage USACO problems
# Usage:
# usaco new <name> [io-name] [--url <link>]
# Create solving/<name> from template.cpp
# (io-name sets freopen filenames, defaults to <name>)
# usaco run <name> Build main.cpp and run it (in the problem dir)
# usaco build <name> Build main.cpp only
# usaco done <name> Move solving/<name> -> solved/<name> and git commit
# usaco undo <name> Move solved/ or skipped/<name> -> solving/<name>
# usaco skip <name> Move solving/<name> -> skipped/<name>
# usaco vim <name> Open the problem's main.cpp in $EDITOR (aliases: edit, e)
# usaco open <name> Open the problem's saved URL in a browser
# usaco list List problems by status
# usaco stats Show solve counts and recent activity
# usaco path <name> Print the problem's directory (used by the ucd shell helper)
# usaco clean Delete compiled binaries and backups from all problem dirs
# usaco template <name> Re-copy template.cpp into the problem as main.cpp (backs up old one)
#
# Names may be unique prefixes: 'usaco e diam' finds diamond-collector.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra)
usage() { sed -n '2,21p' "$0" | sed 's/^# \{0,1\}//'; exit 1; }
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
local name="$1" s d matches=()
for s in solving solved skipped; do
if [[ -d "$ROOT/$s/$name" ]]; then echo "$ROOT/$s/$name"; return 0; fi
done
for s in solving solved skipped; do
for d in "$ROOT/$s/$name"*/; do
[[ -d "$d" ]] && matches+=("${d%/}")
done
done
if [[ ${#matches[@]} -eq 1 ]]; then echo "${matches[0]}"; return 0; fi
if [[ ${#matches[@]} -gt 1 ]]; then
echo "Error: '$name' is ambiguous:" >&2
printf ' %s\n' "${matches[@]#$ROOT/}" >&2
return 1
fi
echo "Error: problem '$name' not found in solving/, solved/, or skipped/" >&2
return 1
}
build_problem() { # $1 = problem dir
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
}
cmd="${1:-}"; shift || true
case "$cmd" in
new)
[[ $# -ge 1 ]] || usage
name=""; io=""; url=""
while [[ $# -gt 0 ]]; do
case "$1" in
--url) url="${2:-}"; shift 2 ;;
*) if [[ -z "$name" ]]; then name="$1"; elif [[ -z "$io" ]]; then io="$1"; fi; shift ;;
esac
done
[[ -n "$name" ]] || usage
io="${io:-$name}"
dir="$ROOT/solving/$name"
if [[ -d "$dir" || -d "$ROOT/solved/$name" || -d "$ROOT/skipped/$name" ]]; then
echo "Error: '$name' already exists" >&2; exit 1
fi
mkdir -p "$dir"
# insert STDIO("<io>"); at the top of main() so file IO is wired up
sed "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$ROOT/template.cpp" > "$dir/main.cpp"
touch "$dir/$io.in"
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
echo "Created solving/$name (io: $io.in / $io.out)"
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
;;
path)
[[ $# -ge 1 ]] || usage
find_problem "$1"
;;
clean)
for s in solving solved skipped; do
[[ -d "$ROOT/$s" ]] || continue
find "$ROOT/$s" -mindepth 2 -maxdepth 2 -type f \
\( -name main -o -name a.out -o -name '*.bak' \) -print -delete | sed "s|^$ROOT/|removed |"
done
;;
build)
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
build_problem "$dir"
echo "Built $dir/main"
;;
run)
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
build_problem "$dir"
# run from inside the dir so freopen finds the .in file
(cd "$dir" && ./main)
# show the .out file if the program wrote one
out="$(ls "$dir"/*.out 2>/dev/null | grep -v '/a\.out$' | head -1 || true)"
if [[ -n "$out" ]]; then
echo "── $(basename "$out") ──"
cat "$out"
fi
;;
done)
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; }
[[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; }
mv "$ROOT/solving/$name" "$ROOT/solved/$name"
echo "Moved to solved/$name 🎉"
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git -C "$ROOT" add -A "solved/$name" "solving/$name" 2>/dev/null || true
git -C "$ROOT" commit -m "Solve $name" >/dev/null && echo "Committed: Solve $name"
fi
;;
undo)
[[ $# -ge 1 ]] || usage
name="$1"; from=""
for s in solved skipped; do
[[ -d "$ROOT/$s/$name" ]] && from="$s" && break
done
[[ -n "$from" ]] || { echo "Error: '$name' not found in solved/ or skipped/" >&2; exit 1; }
[[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; }
mv "$ROOT/$from/$name" "$ROOT/solving/$name"
echo "Moved back to solving/$name (was $from)"
;;
skip)
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; }
[[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; }
mkdir -p "$ROOT/skipped"
mv "$ROOT/solving/$name" "$ROOT/skipped/$name"
echo "Moved to skipped/$name (usaco undo $name to bring it back)"
;;
vim|edit|e)
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
# run from inside the dir so :!./main etc. work and .in files are at hand
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
;;
open)
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
[[ -f "$dir/.url" ]] || { echo "Error: no URL saved for '$1' (use --url with 'new', or write $dir/.url)" >&2; exit 1; }
xdg-open "$(cat "$dir/.url")" >/dev/null 2>&1 &
echo "Opening $(cat "$dir/.url")"
;;
list)
list_by_mtime() { # list subdirs of $1 oldest-first by last modification
[[ -d "$1" ]] || return 0
for d in "$1"/*/; do
[[ -d "$d" ]] || continue
# newest mtime of any file in the problem dir
printf '%s\t%s\n' "$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1)" "$(basename "$d")"
done | sort -n | cut -f2 | sed 's/^/ /'
}
echo "== Solving =="
list_by_mtime "$ROOT/solving"
echo
echo "== Solved =="
list_by_mtime "$ROOT/solved"
if [[ -d "$ROOT/skipped" && -n "$(ls -A "$ROOT/skipped" 2>/dev/null)" ]]; then
echo
echo "== Skipped =="
list_by_mtime "$ROOT/skipped"
fi
;;
stats)
count() { [[ -d "$1" ]] && find "$1" -mindepth 1 -maxdepth 1 -type d | wc -l || echo 0; }
solved=$(count "$ROOT/solved"); solving=$(count "$ROOT/solving"); skipped=$(count "$ROOT/skipped")
now=$(date +%s); week=0; month=0
if [[ -d "$ROOT/solved" ]]; then
for d in "$ROOT/solved"/*/; do
[[ -d "$d" ]] || continue
t="$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1)"
[[ -z "$t" ]] && continue
age=$(( now - t ))
(( age <= 7*86400 )) && week=$((week+1))
(( age <= 30*86400 )) && month=$((month+1))
done
fi
echo "Solved: $solved"
echo "Solving: $solving"
echo "Skipped: $skipped"
echo
echo "Solved in last 7 days: $week"
echo "Solved in last 30 days: $month"
;;
template)
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak" && echo "Backed up old main.cpp -> main.cpp.bak"
cp "$ROOT/template.cpp" "$dir/main.cpp"
echo "Refreshed $dir/main.cpp from template"
;;
*) usage ;;
esac
+55
View File
@@ -0,0 +1,55 @@
# usaco.zsh - zsh integration for the usaco script
# Source this from your ~/.zshrc:
# source ~/Coding/USACO/usaco.zsh
# Provides:
# - tab completion for commands and problem names
# - ucd <name>: cd into a problem's directory
USACO_ROOT="${0:A:h}"
usaco() { "$USACO_ROOT/usaco" "$@"; }
ucd() {
local dir
dir="$("$USACO_ROOT/usaco" path "$1")" || return 1
cd "$dir"
}
_usaco_problems() {
local -a names
names=("$USACO_ROOT"/{solving,solved,skipped}/*(N/:t))
_describe 'problem' names
}
_usaco() {
local -a cmds
cmds=(
'new:create a problem from template'
'run:build and run main.cpp'
'build:build main.cpp'
'vim:open main.cpp in $EDITOR'
'edit:open main.cpp in $EDITOR'
'e:open main.cpp in $EDITOR'
'done:mark solved (auto-commits)'
'undo:move back to solving'
'skip:set aside for later'
'open:open the saved problem URL'
'list:list problems by status'
'stats:show solve counts'
'path:print problem directory'
'clean:delete binaries and backups'
'template:reset main.cpp from template'
)
if (( CURRENT == 2 )); then
_describe 'command' cmds
elif (( CURRENT == 3 )); then
case "$words[2]" in
new|list|stats|clean) ;;
*) _usaco_problems ;;
esac
fi
}
if (( $+functions[compdef] )); then
compdef _usaco usaco
compdef _usaco_problems ucd
fi