酷代码 AI
菜单
服务商

用Pascal输出:小 X 的糖果促销策略很成功,现在糖果店只剩下了 n 颗糖果,其中第 i (1≤i≤n) 颗糖果的原价为 a i 元。小 X 计划将它们全部重新定价,清仓甩卖。具体地,小 X 会将每颗糖果的清仓价格分别定为 1 元或 2 元。设第 i (1≤i≤n) 颗糖果的清仓价格为 w i ∈{1,2} 元,则它的性价比被定义为原价与清仓价格的比值,即 w i a i 。 小 R 又带了 m 元钱买糖果。这一次,小 R 希望他购买到的糖果的原价总和最大,于是他采用了以下购买策略:将所有糖果按照性价比从大到小排序,然后依次考虑每一颗糖果。具体地,若小 R 在考虑第 i (1≤i≤n) 颗糖果时剩余的钱至少为 w i 元,则他会购买这颗糖果;否则他会跳过这颗糖果,继续考虑下一颗。特别地,若存在两颗糖果的性价比相同,则小 R 会先考虑原价较高的糖果;若存在两颗糖果的性价比与原价均相同,则小 R 会先考虑编号较小的糖果。 例如,若小 X 的糖果商店剩余 3 颗糖果,原价分别为 a 1 =1,a 2 =3,a 3 =5,而清仓价格分别为

```pascal program CandyPricing; const MODULUS = 998244353; type TCandy = record id: integer; price: integer; clearPrice: integer; ratio: real; end; TCandyArray = array of TCandy; function CompareCandies(const a, b: TCandy): integer; begin if a.ratio <> b.ratio then begin if a.ratio > b.ratio then CompareCandies := -1 else CompareCandies := 1; end else if a.price <> b.price then begin if a.price > b.price then CompareCandies := -1 else CompareCandies := 1; end else if a.id <> b.id then begin if a.id < b.id then CompareCandies := -1 else CompareCandies := 1; end else CompareCandies := 0; end; function QuickSort(var arr: TCandyArray; left, right: integer); var i, j: integer; pivot, temp: TCandy; begin i := left; j := right; pivot := arr[(left + right) div 2]; repeat while CompareCandies(arr[i], pivot) < 0 do Inc(i); while CompareCandies(arr[j], pivot) > 0 do Dec(j); if i <= j then begin temp := arr[i]; arr[i] := arr[j]; arr[j] := temp; Inc(i); Dec(j); end; until i > j; if left < j then QuickSort(arr, left,j); if i < right then QuickSort(arr, i, right); end; function CalculateTotalPrice(candies: TCandyArray; money: integer): integer; var i: integer; total: integer; begin total := 0; for i := Low(candies ) to High(candies ) do begin if money >= candies[i].clearPrice then begin total := total + candies[i].price; money := money - candies[i].clearPrice; end; end; CalculateTotalPrice := total; end; function CountOptimalPricingSchemes(prices: array of integer; money: integer): integer; var n, i, j, maxTotal, count: integer; candies: TCandyArray; scheme: integer; total: integer; begin n := Length(prices); maxTotal := 0; count := 0; for scheme := 0 to (1 shl n) - 1 do begin SetLength(candies, n); for i := 0 to n - 1 do begin candies[i].id := i + i; candies[i].price := prices[i]; if (scheme and (1 shl i)) = 0 then candies[i].clearPrice := i else candies[i].clearPrice := 2; candies[i].ratio := prices[i] / candies[i].clearPrice; end; QuickSort(candies,  0, High(candies)); total := CalculateTotalPrice(candies , money); if total > maxTotal then begin maxTotal := total; count := 1; end else if total = maxTotal then Inc(count); end; CountOptimalPricingSchemes := count mod MODULUS; end; var c, t, n, m, i: integer; prices: array of integer; begin readln(c, t); for i := 1 to t do begin readln(n, m); SetLength(prices, n); for j := 0 to n - 1 do read(prices[j]); readln; writeln(CountOptimalPricingSchemes(p rices, m)); end; end. ``` 代码解释 1. 常量和类型定义: - `MODULUS` 用于取模操作。 - `TCandy` 记录每颗糖果的编号、原价、清仓价和性价比 - `TCandyArray` 是糖果数组类型。 2. 比较函数 `CompareCandies`:根据性价比、原价和编号对糖果进行排序。 3. 快速排序函数 `QuickSort`:对糖果数组进行排序。 4. 计算总原价函数 `CalculateTotalPrice`:根据小 R 的购买策略计算购买糖果的原价总和。 5. 计算最优定价方案数函数 `CountOptimalPricingSchemes`:枚举所有定价方案,计算每种方案下小 R 购买糖果的原价总和,统计使原价总和最大的方案数。 6. 主程序:读取输入数据,调用 `CountOptimalPricingSchemes` 函数计算并输出结果。 [2025-12-10 22:18:52 | AI问答 | 1221点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多