一、覆盖点集
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
int min_x=INT_MAX,max_x=INT_MIN;
int min_y=INT_MAX,max_y=INT_MIN;
while(cin>>x>>y)
{
if(x==0&&y==0)
break;
min_x=min(min_x,x),max_x=max(max_x,x);
min_y=min(min_y,y),max_y=max(max_y,y);
}
printf("%d %d %d %d",min_x,min_y,max_x,max_y);
return 0;
}
二、回文串
递归
#include<bits/stdc++.h>
using namespace std;
bool judge(string s,int st,int ed)
{
if(st>=ed)
return true;
if(s[st]==s[ed])
return judge(s,st+1,ed-1);
return false;
}
int main()
{
string s;
getline(cin,s);
if(judge(s,0,s.size()-1))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
return 0;
}
非递归
#include<bits/stdc++.h>
using namespace std;
bool judge(string str)
{
stack<char>s;
for(char x : str)
s.push(x);
for(char x : str)
{
if(x!=s.top())
return false;
s.pop();
}
return true;
}
int main()
{
string s;
getline(cin,s);
if(judge(s))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
return 0;
}
三、逆序对的数量(暴力可得大部分分)
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n,a[N],w[N];
typedef long long LL;
LL num(int l,int r)
{
if(l>=r)return 0;
int mid=l+r>>1;
LL res=0;
res+=num(l,mid)+num(mid+1,r);
int i=l,j=mid+1,k=0;
while(i<=mid&&j<=r)
{
if(a[i]<=a[j])w[k++]=a[i++];
else
w[k++]=a[j++],res+=mid-i+1;
}
while(i<=mid)w[k++]=a[i++];
while(j<=r)w[k++]=a[j++];
for(int i=l,j=0;j<k;i++,j++)
a[i]=w[j];
return res;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<num(0,n-1);
return 0;
}