正文
增加 7 行删除 7 行
60 行未改动6162636465666768698 行未改动787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151166 行未改动60 行未改动for(int i = 1; i <= n; i++) {cin >> a[i];}sort(a + 1, a + 1 + n); // 先排序// 将相同数字分组计数groupCount = 0;for(int i = 1; i <= n; i++) {if(a[i] != a[i-1]) { // 新数字8 行未改动// 解决问题的主函数void solve() {int left = 1, right = groupCount; // 双指针初始化operationCount = 0;while(right - left > 1) { // 当还有超过2种数字时继续// 取左右两边较小的数量进行操作int minCount = min(groups[left].count, groups[right].count);// 操作左边:把所有最小值转为第二小的值groups[left+1].count += minCount;groups[left].count -= minCount;operationCount += minCount;// 如果最小值已经处理完,移动左指针if(groups[left].count == 0) {left++;// 特殊处理:如果只剩两种数字了if(right - left == 1) {operationCount += minCount - 1;break;}}// 操作右边:把所有最大值转为第二大的值groups[right-1].count += minCount;groups[right].count -= minCount;operationCount += minCount;// 如果最大值已经处理完,移动右指针if(groups[right].count == 0) {right--;}}// 输出结果cout << operationCount << " " << groups[left].value << " " << groups[right].value;}6 行未改动60 行未改动6162636465666768698 行未改动787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151166 行未改动60 行未改动for(int i = 1; i <= n; i++) {cin >> a[i];}sort(a + 1, a + 1 + n); // 先排序// 将相同数字分组计数groupCount = 0;for(int i = 1; i <= n; i++) {if(a[i] != a[i-1]) { // 新数字8 行未改动// 解决问题的主函数void solve() {int left = 1, right = groupCount; // 双指针初始化operationCount = 0;while(right - left > 1) { // 当还有超过2种数字时继续// 取左右两边较小的数量进行操作int minCount = min(groups[left].count, groups[right].count);// 操作左边:把所有最小值转为第二小的值groups[left+1].count += minCount;groups[left].count -= minCount;operationCount += minCount;// 如果最小值已经处理完,移动左指针if(groups[left].count == 0) {left++;// 特殊处理:如果只剩两种数字了if(right - left == 1) {operationCount += minCount - 1;break;}}// 操作右边:把所有最大值转为第二大的值groups[right-1].count += minCount;groups[right].count -= minCount;operationCount += minCount;// 如果最大值已经处理完,移动右指针if(groups[right].count == 0) {right--;}}// 输出结果cout << operationCount << " " << groups[left].value << " " << groups[right].value;}6 行未改动