Start sum-of-sortable-integers

This commit is contained in:
usaco site
2026-10-20 10:58:27 +00:00
parent c6b82588b1
commit f15ba26bda
11 changed files with 177 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
https://leetcode.com/problems/sum-of-sortable-integers/
+13
View File
@@ -0,0 +1,13 @@
// driver.h - generated by 'usaco new' from LeetCode's metadata (edit freely).
// Reads sum-of-sortable-integers.in in LeetCode's testcase format, cases back to back:
// one argument per line
// and writes one answer per case to sum-of-sortable-integers.out, formatted the way LeetCode
// shows its output. Parsing/printing helpers: leetcode.h.
int main() {
freopen("sum-of-sortable-integers.in", "r", stdin);
freopen("sum-of-sortable-integers.out", "w", stdout);
for (vector<string> in; lc::read_case(in, 1);) {
auto nums = lc::parse<vector<int>>(in[0]);
cout << lc::show(Solution().sortableIntegers(nums)) << '\n';
}
}
+102
View File
@@ -0,0 +1,102 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL // set by ./usaco builds only, so this is safe to submit as-is
#include "debugging.h"
#include "leetcode.h" // ListNode, TreeNode
#endif
// ----- 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 ", "
#define SP " "
// ----- 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 ----- (compiled out on the judge)
#ifdef LOCAL
#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)
#else
#define DBG(vari)
#define DBG2(a, b)
#define printA(a, L, R)
#define printV(a)
#endif
// ----- 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
class Solution {
public:
int sortableIntegers(vector<int>& nums) {
}
};
#ifdef LOCAL // runs the examples; see driver.h
#include "driver.h"
#endif
@@ -0,0 +1,52 @@
# 3886. Sum of Sortable Integers
You are given an integer array `nums` of length `n`.
An integer `k` is called **sortable** if `k` **divides** `n` and you can sort `nums` in **non-decreasing** order by sequentially performing the following operations:
- Partition `nums` into **consecutive subarrays** of length `k`.
- **Cyclically rotate each subarray independently** any number of times to the left or to the right.
Return an integer denoting the sum of all possible sortable integers `k`.
**Example 1:**
**Input:** nums = [3,1,2]
**Output:** 3
**Explanation:**​​​​​​​
- For `n = 3`, possible divisors are 1 and 3.
- For `k = 1`: each subarray has one element. No rotation can sort the array.
- For `k = 3`: the single subarray `[3, 1, 2]` can be rotated once to produce `[1, 2, 3]`, which is sorted.
- Only `k = 3` is sortable. Hence, the answer is 3.
**Example 2:**
**Input:** nums = [7,6,5]
**Output:** 0
**Explanation:**
- For `n = 3`, possible divisors are 1 and 3.
- For `k = 1`: each subarray has one element. No rotation can sort the array.
- For `k = 3`: the single subarray `[7, 6, 5]` cannot be rotated into non-decreasing order.
- No `k` is sortable. Hence, the answer is 0.
**Example 3:**
**Input:** nums = [5,8]
**Output:** 3
**Explanation:**​​​​​​​
- For `n = 2`, possible divisors are 1 and 2.
- Since `[5, 8]` is already sorted, every divisor is sortable. Hence, the answer is `1 + 2 = 3`.
**Constraints:**
- `1 <= n == nums.length <= 10^5`
- `1 <= nums[i] <= 10^5`
@@ -0,0 +1,3 @@
[3,1,2]
[7,6,5]
[5,8]
@@ -0,0 +1 @@
[3,1,2]
@@ -0,0 +1 @@
3
@@ -0,0 +1 @@
[7,6,5]
@@ -0,0 +1 @@
0
@@ -0,0 +1 @@
[5,8]
@@ -0,0 +1 @@
3