暑假算法7.7,Day06
第一题
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int index = 0;
int result = 0;
for (int i = 0; i < s.length && index < g.length; i++) {
if (s[i] >= g[index]) {
index++;
result++;
}
}
return result;
}
}
第二题
心算挑战
class Solution {
public int maxmiumScore(int[] cards, int cnt) {
Arrays.sort(cards);
int index = cards.length - 1;
int sum = 0;
while (cnt > 0) {
sum += cards[index--];
cnt--;
}
if (sum % 2 == 0) return sum;
for (int i = index; i >= 0; i--) {
for (int j = index + 1; j < cards.length; j++) {
sum -= cards[j];
sum += cards[i];
if (sum % 2 == 0) return sum;
sum -= cards[i];
sum += cards[j];
}
}
return 0;
}
}
第三题
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int n=grid.length;
int[] rowMax = new int[n];
int[] colMax = new int[n];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid.length;j++){
rowMax[i]=Math.max(grid[i][j],rowMax[i]);
colMax[j]=Math.max(grid[i][j],colMax[j]);
}
}
int ans = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
}
}
第四题
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100005];
void function1()
{
int n;
map<int,int>p;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
p[a[i]]++;
}
for(auto it=p.begin(); it!=p.end(); it++)
{
if(it->second==1)
{
cout<<"-1"<<endl;
return ;
}
}
int left = 1;
for(auto it=p.begin(); it!=p.end(); it++)
{
int right = it->second + left - 1;
cout << right <<" ";
for(int i = left; i < right; i++)
{
cout << i << " ";
}
left += it->second;
}
cout << endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
function1();
}
return 0;
}