Skip to content

Commit 2e602ad

Browse files
authored
Merge branch 'cpinitiative:master' into master
2 parents e6e8ea9 + 12e2f91 commit 2e602ad

File tree

7 files changed

+394
-97
lines changed

7 files changed

+394
-97
lines changed

content/2_Bronze/Complete_Rec.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ appear much less frequently than they once did.
11731173
11741174
```cpp
11751175
vector<int> perm(n);
1176+
iota(begin(perm), end(perm), 1);
11761177
do {
11771178
} while (next_permutation(begin(perm), end(perm)));
11781179
```

content/4_Gold/Conclusion.problems.json

+13
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,19 @@
278278
"site": "AC"
279279
}
280280
},
281+
{
282+
"uniqueId": "cf-2021E2",
283+
"name": "Digital Village (Hard Version)",
284+
"url": "https://codeforces.com/contest/2021/problem/E2",
285+
"source": "CF",
286+
"difficulty": "Hard",
287+
"isStarred": false,
288+
"tags": ["DSU", "DP"],
289+
"solutionMetadata": {
290+
"kind": "autogen-label-from-site",
291+
"site": "CF"
292+
}
293+
},
281294
{
282295
"uniqueId": "cf-1713E",
283296
"name": "Cross Swapping",

solutions/bronze/usaco-1276.mdx

+109
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,115 @@ int main() {
122122
```
123123

124124
</CPPSection>
125+
<JavaSection>
126+
127+
```java
128+
import java.io.*;
129+
import java.util.*;
130+
131+
public class AirConditioningII {
132+
static int N, M;
133+
// {s, t, c}
134+
static List<int[]> cows = new ArrayList<>();
135+
// {a, b, p, m}
136+
static List<int[]> airConditioners = new ArrayList<>();
137+
// uses[i] == true: the i-th air conditioner is used
138+
static boolean[] uses;
139+
// the minimum amount of money needed to keep all cows comfortable
140+
static int minCost = Integer.MAX_VALUE;
141+
142+
/**
143+
* Based on 'uses', determine if the current subset of air conditioners suffices
144+
* the constraints, and if so, update the minimum cost
145+
*/
146+
static void update() {
147+
boolean isFeasible = true;
148+
149+
// iterate through all positions to check if the current subset is feasible
150+
for (int i = 1; i <= 100; i++) {
151+
// iterate through air conditioners to find the current cooling units
152+
int cooling = 0;
153+
for (int j = 0; j < M; j++) {
154+
if (!uses[j]) continue;
155+
int[] ac = airConditioners.get(j);
156+
int a = ac[0], b = ac[1], p = ac[2];
157+
if (a <= i && i <= b) cooling += p;
158+
}
159+
160+
// iterate through cows to find the current cow
161+
int cowRequirement = 0;
162+
for (int j = 0; j < N; j++) {
163+
int[] cow = cows.get(j);
164+
int s = cow[0], t = cow[1], c = cow[2];
165+
if (s <= i && i <= t) {
166+
cowRequirement = c;
167+
break;
168+
}
169+
}
170+
171+
// For each position, the requirement of the cow must be met
172+
if (cooling < cowRequirement) {
173+
isFeasible = false;
174+
break;
175+
}
176+
}
177+
178+
if (isFeasible) {
179+
int cost = 0;
180+
for (int i = 0; i < M; i++) {
181+
if (uses[i]) cost += airConditioners.get(i)[3];
182+
}
183+
minCost = Math.min(minCost, cost);
184+
}
185+
}
186+
187+
/**
188+
* Expand the subset, represented by 'uses', by choosing to (not) use the i-th
189+
* air conditioner
190+
*/
191+
static void search(int i) {
192+
if (i == M) {
193+
update();
194+
} else {
195+
uses[i] = false;
196+
search(i + 1);
197+
uses[i] = true;
198+
search(i + 1);
199+
}
200+
}
201+
202+
public static void main(String[] args) throws IOException {
203+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
204+
StringTokenizer st = new StringTokenizer(br.readLine());
205+
N = Integer.parseInt(st.nextToken());
206+
M = Integer.parseInt(st.nextToken());
207+
208+
for (int i = 0; i < N; i++) {
209+
st = new StringTokenizer(br.readLine());
210+
int s = Integer.parseInt(st.nextToken());
211+
int t = Integer.parseInt(st.nextToken());
212+
int c = Integer.parseInt(st.nextToken());
213+
cows.add(new int[] {s, t, c});
214+
}
215+
216+
for (int i = 0; i < M; i++) {
217+
st = new StringTokenizer(br.readLine());
218+
int a = Integer.parseInt(st.nextToken());
219+
int b = Integer.parseInt(st.nextToken());
220+
int p = Integer.parseInt(st.nextToken());
221+
int m = Integer.parseInt(st.nextToken());
222+
airConditioners.add(new int[] {a, b, p, m});
223+
}
224+
225+
uses = new boolean[M];
226+
search(0);
227+
228+
System.out.println(minCost);
229+
}
230+
}
231+
```
232+
233+
</JavaSection>
125234
<PySection>
126235

127236
```py

solutions/bronze/usaco-965.mdx

+86-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ int main() {
5858
```
5959

6060
</CPPSection>
61-
6261
<JavaSection>
6362

6463
```java
@@ -158,10 +157,10 @@ public class Lineup {
158157
// CodeSnip{Kattio}
159158
}
160159
```
161-
</JavaSection>
162-
163160

161+
</JavaSection>
164162
</LanguageSection>
163+
165164
</Spoiler>
166165

167166
## Implementation
@@ -242,6 +241,90 @@ int main() {
242241
```
243242
244243
</CPPSection>
244+
<JavaSection>
245+
246+
```java
247+
import java.io.*;
248+
import java.util.*;
249+
250+
class Pair<T, U> {
251+
T first;
252+
U second;
253+
254+
public Pair(T first, U second) {
255+
this.first = first;
256+
this.second = second;
257+
}
258+
}
259+
260+
public class Lineup {
261+
private static final int RESTRICT_LEN = 6;
262+
// list of cows, in alphabetical order
263+
private static final List<String> COWS = Arrays.asList(
264+
"Beatrice", "Belinda", "Bella", "Bessie", "Betsy", "Blue", "Buttercup", "Sue");
265+
266+
private static List<List<String>> orderings = new ArrayList<>();
267+
268+
private static void build(List<String> ordering) {
269+
// finished building permutation
270+
if (ordering.size() == 8) {
271+
orderings.add(new ArrayList<>(ordering));
272+
return;
273+
}
274+
275+
for (String cow : COWS) {
276+
if (!ordering.contains(cow)) {
277+
ordering.add(cow);
278+
build(ordering);
279+
ordering.remove(ordering.size() - 1);
280+
}
281+
}
282+
}
283+
284+
// returns index of a cow in an ordering
285+
private static int loc(List<String> order, String cow) {
286+
return order.indexOf(cow);
287+
}
288+
289+
public static void main(String[] args) throws IOException {
290+
BufferedReader br = new BufferedReader(new FileReader("lineup.in"));
291+
292+
int n = Integer.parseInt(br.readLine().trim());
293+
List<Pair<String, String>> restrictions = new ArrayList<>();
294+
295+
for (int i = 0; i < n; i++) {
296+
String[] words = br.readLine().split(" ");
297+
restrictions.add(new Pair<>(words[0], words[words.length - 1]));
298+
}
299+
300+
br.close();
301+
302+
// build all possible orderings of cows
303+
build(new ArrayList<>());
304+
305+
PrintWriter pw =
306+
new PrintWriter(new BufferedWriter(new FileWriter("lineup.out")));
307+
for (List<String> order : orderings) {
308+
boolean ok = true;
309+
for (Pair<String, String> rule : restrictions) {
310+
if (Math.abs(loc(order, rule.first) - loc(order, rule.second)) > 1) {
311+
ok = false;
312+
break;
313+
}
314+
}
315+
316+
if (ok) {
317+
for (String cow : order) { pw.println(cow); }
318+
break;
319+
}
320+
}
321+
322+
pw.close();
323+
}
324+
}
325+
```
326+
327+
</JavaSection>
245328
<PySection>
246329
247330
```py

0 commit comments

Comments
 (0)