和最大
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int ans=0;
int n,m;
cin >> n >> m;
int a[20][20];
for(int i=0;i<n;i++){
int num=0;
for(int j=0;j<m;j++){
cin >>a[i][j];
num=max(a[i][j],num);
}
ans+=num;
}
cout << ans << endl;
return 0;
}
分牌问题
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a[1000];
int sum=0;
int n;
cin >> n;
for(int i=0;i<n;i++){
cin >> a[i];
sum+=a[i];//统计一下总数
}
sum/=n;//计算平均值
for(int i=0;i<n;i++)//更新数组
a[i]-=sum;
int now=0;//记录需要往后面送多少张卡牌
int ans=0;
for(int i=0;i<n;i++){
now+=a[i];
if(now!=0)//如果结果不为0证明该堆还需要往后送牌
ans++;//更新答案
}
cout << ans << endl;
return 0;
}
删数问题
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int,int>pi;
int main(){
string s;//读入字符串
int n;//读入要删掉的数字个数
cin >> s >> n;
int len=s.length()-n;//最后要输出的数字个数
for(int i=0;i<n;i++){//开始删数
for(int j=0;j<s.length()-1;j++){//如果当前位置比后面一个数字大就删除并寻找下一个要删掉的数字
if(s[j]>s[j+1]){
s.erase(s.begin()+j);
break;
}
}
}
bool flag=true;
for(int i=0;i<len;i++){
if(s[i]!='0') *****//去除多余的0
flag=false;
if(flag)
continue;
cout << s[i];
}
return 0;
}
导弹拦截
#include <bits/stdc++.h>
using namespace std;
int a[30005];
int main(){
int x;
int count=0;//答案
while(cin >> x){
if(x>a[count]){//如果当年的导弹高度比所有系统都高
count++;
a[count]=x;
}
else{//其他情况寻找比他刚刚大的最小的数字
int p=lower_bound(a,a+count+1,x)-a;
// cout << p << endl;
a[p]=x;//更新该防御系统
for(int i=p;i>0;i--){//维护系统的递增性质
if(a[i]<a[i-1])
swap(a[i],a[i-1]);
else
break;
}
}
}
cout << count << endl;//输出答案
return 0;
}
活动选择
#include <iostream>
#include <queue>
using namespace std;
struct node{
int s,e;
bool operator < (const node p) const{//重载小于号让结束的晚的比较小
return e>p.e;
}
};
int main(){
priority_queue<node>q;//大根堆
int n;
cin >> n;
while(n--){//将所有节点读入大根堆
int s,e;
cin >> s >> e;
node p={s,e};
q.push(p);
}
int now=0;
int ans=0;
while(!q.empty()){
node p=q.top();
q.pop();
if(p.s>=now){//如果当前节目开始时间比上一个节目结束时间晚
now=p.e;//更新答案和结束时间
ans++;
}
}
cout << ans <<endl;
return 0;
}
整数区间
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int s,e;
}p[10005];//结构体数组
bool cmp(node x,node y){//自定义sort比较规则
if(x.e==y.e)
return x.s<=y.s;
return x.e<y.e;
}
int main (){
int n;
cin >> n;
for(int i=0;i<n;i++){//读入所有区间
int s,e;
cin >> s >> e;
p[i]={s,e};
}
sort(p,p+n,cmp);//排序
int ans=1;
int now=p[0].e;
for(int i=1;i<n;i++){
// cout << p[i].s << ' ' <<p[i].e << endl;
if(p[i].s>now){//如果该区间的起点比终点短更新答案
ans++;
now=p[i].e;
}
}
cout << ans << endl;
return 0;
}
最大整数
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
string s[25];
for(int i=0;i<n;i++)
cin >> s[i];//读入所有字符串
for(int i=0;i<n-1;i++){
for(int j=i;j<n;j++){
if(s[i]+s[j]<s[j]+s[i])//比较谁排在前面比较大输出答案
swap(s[i],s[j]);
}
}
for(int i=0;i<n;i++)
cout << s[i];
return 0;
}
纪念品分组
#include<iostream>
#include<algorithm>
using namespace std;
int a[205];
int main(){
int w;//读入权重值的上限
cin >> w;
int n;//读入n
cin >> n;
while(n--){//读入n个物品的重量
int x;
cin >> x;
a[x]++;//将该物品标记为已经使用过
}
int ans=0;
for(int i=0;i<=w;i++){
while(a[i]>0){//如果该物品没有用完
ans++;
a[i]--;
bool flag=false;
for(int j=w-i;j>=0;j--){//寻找与该物品能够在一个组内的最大值
if(a[j]){//使用完这个物品数量减一
a[j]--;
break;
}
}
}
}
cout << ans << endl;
return 0;
}
合并果子
#include<iostream>
#include<algorithm>
#include <queue>
using namespace std;
priority_queue<int ,vector<int>,greater<int> >q;//小根堆
int main(){
int n;
cin >> n;//读入n
while(n--){
int x;
cin >> x;
q.push(x);//将x加入小根堆当中
}
int ans=0;
while(q.size()!=1){
int a=q.top();
q.pop();
int b=q.top();
q.pop();
ans+=(a+b);//合并最小的两堆压入堆顶
q.push(a+b);
}
cout << ans << endl;//输出答案
return 0;
}
马克与美元
#include<iostream>
#include<algorithm>
#include <queue>
using namespace std;
int main(){
int n;
cin >> n;
double a[105];
double sum=100;
for(int i=0;i<n;i++)
cin >> a[i];
for(int i=0;i<n-1;i++){//寻找马克比跌的两天更新答案
if(a[i+1]<a[i])
sum=sum*a[i]/a[i+1];
}
printf("%.2lf",sum);
return 0;
}
标准部件
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int,int>pi;
bool st[1005];
pi p[1005];
int main(){
int n;
cin >> n;
for(int i=0;i<n;i++){
int x,y;
cin >> x >> y;
p[i]={x,y};//存入二元组当中
}
sort(p,p+n);//二元组排序
int ans=0;
for(int i=0;i<n;i++){
if(!st[i]){//如果这个物品没被用过
st[i]=true;
ans++;
int nowx=p[i].first;
int nowy=p[i].second;
for(int j=i+1;j<n;j++){//去寻找可以和这个物品匹配的下一个物品
if(st[j])
continue;
if(p[j].first>=nowx && p[j].second>=nowy){
st[j]=true;//找到了更新nowx和nowy,并标记为使用过
nowx=p[j].first;
nowy=p[j].second;
}
}
}
}
cout << ans << endl;
return 0;
}
胖子计划
#include<iostream>
#include<algorithm>
#include <queue>
using namespace std;
struct node{
int x;
int w;
bool operator < (const node p) const {
return w<=p.w;
}
};//存储套餐编号和热量
priority_queue<node>q;
int st[205];//存储每个套餐可以还可以吃几次
int main(){
int n,m,k;
cin >> n >> m >> k;
for(int i=1;i<=k;i++)
cin >> st[i];//读入套餐量
for(int i=0;i<n;i++){//读入套餐种类
int x,w;
cin >> w >> x;
node p={x,w};
q.push(p);
}
int ans=0;
// while(!q.empty()){
// node p=q.top();
// q.pop();
// cout << p.x << ' ' << p.w << endl;
// }
while(1){
if(m==0 || q.empty())// 如果已经吃完m个套餐或者没套餐吃了
break;
node p=q.top();
q.pop();
if(st[p.x]){//判断一下当前最高热量套餐还能不能吃
st[p.x]--;
ans+=p.w;
// cout << p.x << ' ' << p.w << endl;
m--;
}
}
cout << ans << endl;//输出答案
return 0;
}
便利店
#include<iostream>
#include<algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int main(){
ll a,b,c,d;
ll n;
cin >> a >> b >> c >> d;
cin >> n;
ll num1=min(a*4,2*b);//求出一升饮料的最小单价
num1=min(c,num1);
ll num2=min(d,2*num1);//求出两升饮料的最小单价
cout << (n/2)*num2+(n%2)*num1 << endl;//输出答案
return 0;
}