NOIP 2006 Competition Problems: Solutions and Analysis
Problem 1: Energy Necklace (vijos1312)
Problem Statement
On the Mars planet, every inhabitant wears an energy necklace containing N beads. Each bead has a head tag and a tail tag, both represented by positive integers. For two adjacent beads, the tail tag of the first bead must equal the head tag of the second bead. This constraint ensures that the beads can be merged using a absorbing organ, releasing energy in the process.
When a bead with head tag m and tail tag r merges with a bead having head tag r and tail tag n, the released energy equals m×r×n (in Mars units), and a new bead is created with head tag m and tail tag n.
Given a circular necklace of N beads, the task is to merge adjacent beads repeatedly until only one bead remains. Different merging sequences produce different total energy outputs. Find the maximum possible total energy.
Input Format
- First line: integer N (4 ≤ N ≤ 100)
- Second line: N integers representing head tags. The i-th integer corresponds to the i-th bead's head tag. For 1 ≤ i < N, the i-th bead's tail tag equals the (i+1)-th bead's head tag. The N-th bead's tail tag equals the 1st bead's head tag.
Output Format
- Single integer E (E ≤ 2.1×10⁹) representing the maximum total energy obtainable.
Example
Input:
4
2 3 5 10
Output:
710
Solution: Circular Dynamic Programming
Since the necklace is circular and N ≤ 100, we duplicate the sequence to handle wrap-around cases. For beads indexed 1 to N, let arr[i] represent the head tag of the i-th bead, with arr[N+1] = arr[1] (the circular connection).
We define dp[i][j] as the maximum energy obtainable from merging beads from position i to position j (inclusive) into a single bead.
The state transition considers all possible final merge operations:
dp[i][j] = max(dp[i][j], arr[i] * arr[k+1] * arr[j+1] + dp[i][k] + dp[k+1][j])
where k ranges from i to j-1, representing the last merge performed.
We process states in order of increasing length, ensuring subproblems are resolved before larger ones. The answer is the maximum dp value across all length-N windows in the duplicated array.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int N;
std::cin >> N;
std::vector<int> beads(2 * N + 2);
for (int i = 1; i <= N; i++) {
std::cin >> beads[i];
beads[i + N] = beads[i];
}
std::vector<std::vector<int>> dp(2 * N + 2, std::vector<int>(2 * N + 2, 0));
int maxEnergy = 0;
for (int length = 1; length <= 2 * N; length++) {
for (int start = 1; start + length - 1 <= 2 * N; start++) {
int end = start + length - 1;
if (start == end) {
dp[start][end] = 0;
} else {
for (int k = start; k < end; k++) {
int energy = beads[start] * beads[k + 1] * beads[end + 1]
+ dp[start][k] + dp[k + 1][end];
dp[start][end] = std::max(dp[start][end], energy);
}
}
if (length == N) {
maxEnergy = std::max(maxEnergy, dp[start][end]);
}
}
}
std::cout << maxEnergy << std::endl;
return 0;
}
Problem 2: Shopping Budget (vijos1313)
Problem Statement
Jin Ming is decorating his new room with a budget constraint. He categorizes desired items into main items and their accessories. To purchase an accessory, the corresponding main item must be purchased first. Each main item can have 0, 1, or 2 accessories, and accessories have no sub-accessories.
Each item has a price (multiple of 10) and an importance rating (1-5). Jin Ming wants to maximize the sum of (price × importance) for selected items without exceeding the budget N.
Input Format
- First line: N m (N < 32000 is total budget, m < 60 is number of items)
- Following m lines: v p q where v is price, p is importance, q indicates main (q=0) or accessory (q>0 is parent main item's index)
Output Format
- Maximum achievable value of Σ(price × importance)
Example
Input:
1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0
Output:
2200
Solution: Grouped Knapsack with Dependencies
This is a grouped knapsack problem where each main item and its accessories form a group. For each group, we can select:
- Only the main item
- Main item + first accessory
- Main item + second accessory
- Main item + both accessories
Prices are divided by 10 to reduce memory usage, and the final answer is multiplied by 10.
#include <iostream>
#include <vector>
struct ItemGroup {
int mainPrice, mainWeight;
int acc1Price, acc1Weight;
int acc2Price, acc2Weight;
};
int main() {
int N, m;
std::cin >> N >> m;
N /= 10; // Scale down prices
std::vector<ItemGroup> groups;
std::vector<int> tempPrice(m + 1), tempWeight(m + 1), tempParent(m + 1);
for (int i = 1; i <= m; i++) {
int v, p, q;
std::cin >> v >> p >> q;
tempPrice[i] = v / 10;
tempWeight[i] = p;
tempParent[i] = q;
}
// Organize into groups
for (int i = 1; i <= m; i++) {
if (tempParent[i] == 0) {
ItemGroup g;
g.mainPrice = tempPrice[i];
g.mainWeight = tempWeight[i];
g.acc1Price = g.acc1Weight = 0;
g.acc2Price = g.acc2Weight = 0;
for (int j = i + 1; j <= m; j++) {
if (tempParent[j] == i) {
if (g.acc1Price == 0) {
g.acc1Price = tempPrice[j];
g.acc1Weight = tempWeight[j];
} else {
g.acc2Price = tempPrice[j];
g.acc2Weight = tempWeight[j];
}
}
}
groups.push_back(g);
}
}
std::vector<std::vector<int>> dp(groups.size() + 1, std::vector<int>(N + 1, 0));
for (size_t i = 1; i <= groups.size(); i++) {
ItemGroup& g = groups[i - 1];
for (int j = 1; j <= N; j++) {
// Option 1: Main item only
if (j >= g.mainPrice) {
dp[i][j] = std::max(dp[i][j],
dp[i - 1][j - g.mainPrice] + g.mainPrice * g.mainWeight);
}
// Option 2: Main + accessory 1
if (g.acc1Price > 0 && j >= g.mainPrice + g.acc1Price) {
dp[i][j] = std::max(dp[i][j],
dp[i - 1][j - g.mainPrice - g.acc1Price]
+ g.mainPrice * g.mainWeight + g.acc1Price * g.acc1Weight);
}
// Option 3: Main + accessory 2
if (g.acc2Price > 0 && j >= g.mainPrice + g.acc2Price) {
dp[i][j] = std::max(dp[i][j],
dp[i - 1][j - g.mainPrice - g.acc2Price]
+ g.mainPrice * g.mainWeight + g.acc2Price * g.acc2Weight);
}
// Option 4: Main + both accessories
if (g.acc1Price > 0 && g.acc2Price > 0
&& j >= g.mainPrice + g.acc1Price + g.acc2Price) {
dp[i][j] = std::max(dp[i][j],
dp[i - 1][j - g.mainPrice - g.acc1Price - g.acc2Price]
+ g.mainPrice * g.mainWeight + g.acc1Price * g.acc1Weight
+ g.acc2Price * g.acc2Weight);
}
// Option 5: Skip this group
dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
}
}
std::cout << dp[groups.size()][N] * 10 << std::endl;
return 0;
}
Problem 3: Machine Scheduling (vijos1314)
Problem Statement
Given m machines and n jobs, where each job has m operations that must be performed on specific machines in a fixed order. An operation sequence is predetermined. Each operation must wait for its prerequisite operation to complete before starting, and each machine can process only one operation at a time.
When scheduling an operation, insert it into the earliest available time slot on its required machine that satisfies all constraints. Calculate the total completion time for all operations.
Input Format
- First line: m n (m, n < 20)
- Second line: m×n integers representing the operation sequence
- Next n lines: Machine numbers for each operation of each job
- Final n lines: Processing time for each operation of each job
Output Format
- Minimum total processing time required
Example
Input:
2 3
1 1 2 3 3 2
1 2
1 2
2 1
3 2
2 5
2 4
Output:
10
Solution: Greedy Time Slot Simulation
Maintain a timeline for each machine. For each operation in sequence order:
- Find the earliest continuous time slot of required duration
- Mark that slot as occupied
- Update the job's completion time
The algorithm scans forward from the job's previous completion point, counting consecutive empty slots until the required duration is met.
#include <iostream>
#include <cstring>
#include <algorithm>
const int MAX_TIME = 8000;
const int MAX_MACHINES = 25;
int main() {
int m, n;
std::cin >> m >> n;
int seqLen = m * n;
std::vector<int> sequence(seqLen + 1);
for (int i = 1; i <= seqLen; i++) {
std::cin >> sequence[i];
}
std::vector<std::vector<int>> machineReq(n + 1, std::vector<int>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
std::cin >> machineReq[i][j];
}
}
std::vector<std::vector<int>> procTime(n + 1, std::vector<int>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
std::cin >> procTime[i][j];
}
}
// Timeline: 0 = free, 1 = occupied
std::vector<std::vector<int>> timeline(m + 1, std::vector<int>(MAX_TIME, 0));
std::vector<int> jobProgress(n + 1, 0); // Current operation index for each job
std::vector<int> jobFinish(n + 1, 0); // When each job finished
for (int idx = 1; idx <= seqLen; idx++) {
int job = sequence[idx];
jobProgress[job]++;
int opIdx = jobProgress[job];
int machine = machineReq[job][opIdx];
int duration = procTime[job][opIdx];
int consecutive = 0;
for (int t = jobFinish[job] + 1; t < MAX_TIME; t++) {
if (timeline[machine][t] == 0) {
consecutive++;
} else {
consecutive = 0;
}
if (consecutive == duration) {
// Occupy the time slot
for (int s = t; s >= t - consecutive + 1; s--) {
timeline[machine][s] = 1;
}
jobFinish[job] = t;
break;
}
}
}
int maxFinish = 0;
for (int machine = 1; machine <= m; machine++) {
for (int t = MAX_TIME - 1; t >= 0; t--) {
if (timeline[machine][t] == 1) {
maxFinish = std::max(maxFinish, t);
break;
}
}
}
std::cout << maxFinish << std::endl;
return 0;
}
Problem 4: 2^k Base Number Counting (vijos1315)
Problem Statement
Count the number of 2^k base numbers r satisfying:
- r has at least 2 digits in base 2^k
- Each digit (except the last) is strictly less than the digit to its right
- When converted to binary, the result has at most w bits
Given integers k (1 ≤ k ≤ 9) and w (k < W ≤ 30000).
Input Format
- Single line: k W
Output Format
- Count of valid numbers (may be up to 200 digits, output in decimal)
Example
Input:
3 7
Output:
36
Solution: Combinatorial Counting with Big Integers
A w-bit binary string corresponds to a 2^k base number where each digit consumes k bits (except possib the first partial segment). The constraint that each digit must be strictly increasing means we need to count combinations.
Let base = 2^k. For an l-digit base number, the first digit has (base - l + 1) choices, and the total count follows a combinatorial pattern.
For w bits forming segments of k bits:
- Full segments: q = w / k
- Partial segment: r = w % k
The answer involves:
- All l-digit numbers where 2 ≤ l < min(base, q + 1)
- Special cases where the highest digit is restricted by the partial segment
#include <iostream>
#include <cstring>
#include <string>
const int BASE = 100000;
const int CUT = 5;
const int MAX_LEN = 50;
const int MAX_N = 600;
struct BigInt {
int len;
int num[MAX_LEN];
BigInt() {
len = 1;
memset(num, 0, sizeof(num));
}
BigInt& operator=(const BigInt& other) {
len = other.len;
for (int i = 0; i < len; i++) {
num[i] = other.num[i];
}
return *this;
}
};
BigInt operator+(BigInt a, const BigInt& b) {
long long carry = 0;
int maxLen = std::max(a.len, b.len);
for (int i = 0; i < maxLen || carry; i++) {
a.num[i] += carry;
if (i < b.len) a.num[i] += b.num[i];
carry = a.num[i] / BASE;
a.num[i] %= BASE;
}
a.len = maxLen;
while (a.len > 1 && a.num[a.len - 1] == 0) a.len--;
return a;
}
std::istream& operator>>(std::istream& in, BigInt& b) {
std::string s;
in >> s;
int l = s.length();
int pos = 0;
while (pos < l && s[pos] == '0') pos++;
if (pos == l) pos = l - 1;
for (int i = l - 1; i >= pos; i -= CUT) {
int val = 0, mult = 1;
for (int j = 0; j < CUT && i >= pos; j++, i--) {
val += (s[i] - '0') * mult;
mult *= 10;
}
b.num[b.len++] = val;
}
b.len--;
return in;
}
std::ostream& operator<<(std::ostream& out, const BigInt& b) {
out << b.num[b.len - 1];
char buf[CUT + 1];
for (int i = b.len - 2; i >= 0; i--) {
snprintf(buf, sizeof(buf), "%05d", b.num[i]);
out << buf;
}
return out;
}
int main() {
int k, w;
std::cin >> k >> w;
int base = 1 << k;
int fullSegs = w / k;
int partBits = w % k;
int partRange = 1 << partBits;
BigInt comb[MAX_N][MAX_N];
// Precompute combinations C[n][k]
for (int n = 0; n <= base; n++) {
for (int r = 0; r <= n; r++) {
if (r == 0) {
comb[n][r].num[0] = 1;
} else {
comb[n][r] = comb[n - 1][r] + comb[n - 1][r - 1];
}
}
}
BigInt result;
// Count l-digit numbers for 2 ≤ l < min(base, fullSegs + 1)
int maxFull = std::min(base, fullSegs + 1);
for (int l = 2; l < maxFull; l++) {
result = result + comb[base - 1][l];
}
// Add cases with partial segment constraint
for (int high = 1; high < partRange && fullSegs + high < base; high++) {
result = result + comb[base - high - 1][fullSegs];
}
std::cout << result << std::endl;
return 0;
}