A: max(max(a-b,a+b),a*b)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
int a,b,c;
cin>>a>>b;
cout<<max(max(a+b,a-b),a*b);
return 0;
}
B:输出一下x的左右k个数和x
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
cout<<k-n+1;
for (int i = k-n+2;i < k+n;i ++)
cout<<' '<<i;
return 0;
}
C:对每个字符串排序,记录一下个数遍历一下,会爆int
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
unordered_map<string,int> mp;
string s;
ll ans = 0;
for (int i = 0;i < n;i ++)
{
cin>>s;
sort(s.begin(),s.end());
if (!mp[s]) mp[s] = 1;
else mp[s] ++;
}
unordered_map<string,int> :: iterator it;
for (it = mp.begin();it != mp.end();it ++)
{
int n = it->second;
ans += 1LL* n*(n-1)/2;
}
cout<<ans<<endl;
return 0;
}
D:先说题意,一共有n个任务,要在m天完成一些任务,对于这n个任务,每天个已完成一个任务ai,但是要在bi天后才能拿到钱,问最大化能得到多少钱
思路:考虑从最后一天开始做任务,每一天都做得到钱数最多的,优先队列维护(最后一天能领到钱,那么之前的每一天做这个任务,也都能拿到钱)
#include<bits/stdc++.h>
using namespace std;
struct node
{
int d,x;
}a[100010];
bool cmp(node a,node b)
{
return a.d<b.d;
}
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for (int i = 0;i < n;i ++)
cin>>a[i].d>>a[i].x;
sort(a,a + n,cmp);//先按天数升序排序
priority_queue<int> que;
int ans = 0,pos = 0;
for (int i = 1;i <= m;i ++)
{
for (int j = pos;j <= n;j ++)
{
if (a[j].d == i) que.push(a[j].x);
else if (a[j].d > i)
{
pos = j;
break;
}
}
if (!que.empty())
{
ans += que.top();
que.pop();
}
}
cout<<ans<<'\n';
return 0;
}