3 条题解

  • 0
    @ 2025-4-27 19:49:17

    114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514114514

    • 0
      @ 2025-4-10 19:04:00

      //100分的代码:

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <climits>
      
      using namespace std;
      
      int main() {
          int n;
          cin >> n;
        
          vector<pair<int, int>> dominoes(n);
          int top_sum = 0, bottom_sum = 0;
        
          for (int i = 0; i < n; ++i) {
              cin >> dominoes[i].first >> dominoes[i].second;
              top_sum += dominoes[i].first;
              bottom_sum += dominoes[i].second;
          }
        
          int min_diff = abs(top_sum - bottom_sum);
          int min_flips = 0;
        
          vector<vector<int>> dp(n + 1, vector<int>(2 * 6 * n + 1, INT_MAX));
          dp[0][6 * n] = 0;  // 初始状态,差值为0,翻转次数为0
        
          for (int i = 0; i < n; ++i) {
              int a = dominoes[i].first;
              int b = dominoes[i].second;
              int diff = a - b;
            
              for (int j = 0; j <= 2 * 6 * n; ++j) {
                  if (dp[i][j] != INT_MAX) {
                      // 不翻转当前骨牌
                      int new_j = j + diff;
                      if (new_j >= 0 && new_j <= 2 * 6 * n) {
                          if (dp[i + 1][new_j] > dp[i][j]) {
                              dp[i + 1][new_j] = dp[i][j];
                          }
                      }
                    
                      // 翻转当前骨牌
                      new_j = j - diff;
                      if (new_j >= 0 && new_j <= 2 * 6 * n) {
                          if (dp[i + 1][new_j] > dp[i][j] + 1) {
                              dp[i + 1][new_j] = dp[i][j] + 1;
                          }
                      }
                  }
              }
          }
        
          for (int j = 0; j <= 2 * 6 * n; ++j) {
              if (dp[n][j] != INT_MAX) {
                  int current_diff = abs(j - 6 * n);
                  if (current_diff < min_diff) {
                      min_diff = current_diff;
                      min_flips = dp[n][j];
                  } else if (current_diff == min_diff && dp[n][j] < min_flips) {
                      min_flips = dp[n][j];
                  }
              }
          }
        
          cout << min_flips << endl;
          return 0;
      }
      
      • 0
        @ 2025-4-9 20:15:20

        24分的代码:

        #include <iostream>
        #include <vector>
        #include <climits>
        #include <algorithm>
        using namespace std;
        
        // 函数用于生成图腾
        vector<string> generateTotem(int n) {
            if (n == 1) {
                return {" /\\", "/__\\"};
            }
            vector<string> prevTotem = generateTotem(n - 1);
            int prevHeight = prevTotem.size();
            int prevWidth = prevTotem[0].size();
            vector<string> newTotem(2 * prevHeight);
            // 处理上半部分
            for (int i = 0; i < prevHeight; ++i) {
                newTotem[i] = string(prevWidth, ' ') + prevTotem[i];
            }
            // 处理下半部分
            for (int i = 0; i < prevHeight; ++i) {
                newTotem[prevHeight + i] = prevTotem[i] + " " + prevTotem[i];
            }
            return newTotem;
        }
        
        // 多米诺骨牌问题的函数
        int minFlipsToMinDifference(int n, vector<pair<int, int>>& dominoes) {
            int topSum = 0, bottomSum = 0;
            for (const auto& domino : dominoes) {
                topSum += domino.first;
                bottomSum += domino.second;
            }
            int minDiff = INT_MAX;
            int minFlips = 0;
            // 枚举所有可能的翻转组合
            for (int mask = 0; mask < (1 << n); ++mask) {
                int currentTopSum = topSum, currentBottomSum = bottomSum;
                int currentFlips = 0;
                for (int i = 0; i < n; ++i) {
                    if (mask & (1 << i)) {
                        currentTopSum = currentTopSum - dominoes[i].first + dominoes[i].second;
                        currentBottomSum = currentBottomSum - dominoes[i].second + dominoes[i].first;
                        currentFlips++;
                    }
                }
                int currentDiff = abs(currentTopSum - currentBottomSum);
                if (currentDiff < minDiff || (currentDiff == minDiff && currentFlips < minFlips)) {
                    minDiff = currentDiff;
                    minFlips = currentFlips;
                }
            }
            return minFlips;
        }
        
        int main() {
            int n;
            cin >> n;
            vector<pair<int, int>> dominoes(n);
            for (int i = 0; i < n; ++i) {
                cin >> dominoes[i].first >> dominoes[i].second;
            }
            int result = minFlipsToMinDifference(n, dominoes);
            cout << result << endl;
            return 0;
        }
        
        • 1

        信息

        ID
        135
        时间
        1000ms
        内存
        256MiB
        难度
        9
        标签
        递交数
        61
        已通过
        3
        上传者