全部评论
一直20%,赛后才想起来可以两座塔合并成一座塔,这点没考虑。。 太菜了。。
import java.util.Scanner;
public class xxx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int MAX = 0,MIN = 10001;
int MAXx = 0 ,MINy = 0;
int k1 = k;
int [][] num = new int[k][2];
int [] nu = new int[n];
for (int i = 0; i < n; i++) {
nu[i] = sc.nextInt();
MIN = MIN>nu[i]?nu[i]:MIN;
MAX = MAX<nu[i]?nu[i]:MAX;
}
if(MAX!=MIN){
while(k1-->0){
for (int i = 0; i < n; i++) {
if(MAX<=nu[i]){
MAX = nu[i];
MAXx = i;
}
if(MIN>=nu[i]){
MIN = nu[i];
MINy = i;
}
}
if(MAX!=MIN){
MIN = --nu[MAXx];
MAX = ++nu[MINy];
num[k-(k1+1)][0] = MAXx+1;
num[k-(k1+1)][1] = MINy+1;
}else{
break;
}
}
MAX = 0;
MIN = 10001;
for (int i = 0; i < n; i++) {
MIN = MIN>nu[i]?nu[i]:MIN;
MAX = MAX<nu[i]?nu[i]:MAX;
}
System.out.println((MAX-MIN)+" "+(k-(k1+1)));
for (int i = 0; i < k; i++) {
System.out.println(num[i][0]+" "+num[i][1]);
}
}else{
System.out.println("0"+" "+"0");
}
}
}
有java代码吗
//暴力AC了
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,m;
while (cin >> n>>m)
{
int cnt = 1;
vector<pair<int, int>> higher;
vector<pair<int, int>> data;
while (n--)
{
int high;
cin >> high;
pair<int,int> hh;
hh.first = high;
hh.second = cnt;
cnt++;
higher.push_back(hh);
}
sort(higher.begin(), higher.end());
int count = 0;
while (m--)
{
if ((higher.front().first == higher.back().first)&&
((higher.front().first+1) == higher.back().first))
break;
pair<int, int> dd;
higher.back().first--;
higher.front().first++;
dd.first = higher.back().second;
dd.second = higher.front().second;
data.push_back(dd);
sort(higher.begin(), higher.end());
count++;
}
cout << higher.back().first - higher.front().first
<< " " << count << endl;
for (auto i : data)
{
cout << i.first << " " << i.second << endl;
}
}
return 0;
}
我的一个想法是建立最大堆和最小堆,堆内的元素是(i,a[i]),对k个操作改变最大堆和最小堆的极值。不知道这个思路行吗?
一直20%
一直20
同一直20
这个应该贪心就行吧……但是很迷,我这边一直20%
mark 最多才20……想到一个思路,等验证看看……
暴力贪心
马克,我一直是0%。。。
相关推荐
06-02 15:17
门头沟学院 Java 点赞 评论 收藏
分享
07-17 12:09
门头沟学院 Java 点赞 评论 收藏
分享