2022.7.7

1.排序 + 贪心。

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int n1=g.size(),n2=s.size();
        int cnt=0;
        int p=0,q=0;
        while(q<n2 && p<n1){
            if(g[p]<=s[q]){
                cnt++;
                p++;
            }
            q++;
        }
        return cnt;
    }
};

在这里插入图片描述
2.先升序排序,把前ans个数加起来,并记录最后一个奇数和偶数:如果和为偶数,直接返回;如果为奇数,从cnt开始遍历数组,找出第一个奇数和偶数,比较”ans减去前面最后一个奇数加上后面第一个偶数“和”ans减去前面最后一个偶数加上后面第一个奇数“的大小,返回大的。

class Solution {
public:
    int maxmiumScore(vector<int>& cards, int cnt) {
        sort(cards.rbegin(),cards.rend());
        int n=cards.size();
        int ans=0;
        int jishu,oushu;
        for(int i=0;i<cnt;i++){
          ans+=cards[i]; 
          if(cards[i]%2){
              jishu=cards[i];
          } else{
              oushu=cards[i];
          }
        }
        if(ans%2==0){
            return ans;
        }
        int max1=0,max2=0;
        for(int i=cnt;i<n;i++){
            if(max1==0 && cards[i]%2==1){
                max1=ans-oushu+cards[i];
            }
            if(max2==0 && cards[i]%2==0){
                max2=ans-jishu+cards[i];
            }
        }
        return max(max1,max2);
           
    }
        
};

在这里插入图片描述
3.贪心

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        int n=grid.size();
        vector<int>h(n);
        vector<int>l(n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                h[i]=max(h[i],grid[i][j]);
                l[j]=max(l[j],grid[i][j]);
            }
        }
        int ans=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                ans+=min(h[i],l[j])-grid[i][j];
            }
        }
        return ans;
    }
};

在这里插入图片描述
4.交换鞋子的人互相尺码必须相等,不然会出现有人穿不了小鞋的情况,所以只需要找到两个一样尺码的交换即可。

#include <bits/stdc++.h>
using namespace std;
int const N=1e5;
int s[N],o[N];
int main(){
	int t,n;
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>s[j];
		}
		o[N]=s[N];
		sort(s.begin(),s.end());
		int temp=0;
		for(int k=0;k<n;k++){
			if(s[k]==s[k+1]){
				temp=s[k];
				break;
			}
		}
		if(temp==0){
			cout<<-1<<endl;
		}
		int a,b;
		int d=0;
		for(int m=0;m<n;m++){
			if(o[m]==temp && d==0){
				d++;
				a=m;
				continue;
			}
			if(o[m]==temp && d==1){
				d++;
				b=m;
			}
			if(d==2){
			break;
			}
		}
		int q[n];
		for(int p=0;p<n;p++){
			q[p]=p;
		}
		swap(q[a],q[b]);
		cout<<q;
	}
	return 0;
}

在这里插入图片描述

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0x0c……

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值