pair<int,int> a[N];
sort(a+1,a+n+1);
可以对a[i].first进行排序 即对第一个元素进行排序
踩到坑了 还有一种情况
bool cmp(pii a,pii b)//对second元素做要求
{
if(a.first==b.first) return a.second<b.second;
else return a.first<b.first;
}
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> pii;
pii a[10];
bool cmp(pii a,pii b)
{
if(a.first==b.first) return a.second<b.second;
else return a.first<b.first;
}
int main()
{
int n=0,i,j;
while(~scanf("%d%d",&i,&j))
{
n++;
if(i<=j) a[n].first=i,a[n].second=j;
else a[n].first=j,a[n].second=i;
if(n==6)
{
n=0;
//sort(a+1,a+7,cmp);
sort(a+1,a+7);
int k=0;
//判断是不是三对边
for(int i=1;i<=6;i+=2)
{
if(a[i].first==a[i+1].first&&a[i].second==a[i+1].second) k++;
else break;
}
//for(int i=1;i<=6;i++) printf("%d %d\n",a[i].first,a[i].second);
if(k!=3)
{
puts("IMPOSSIBLE");
continue;
}
//for(int i=1;i<=6;i++) printf("%d %d\n",a[i].first,a[i].second);
int res1,res2;//判断正方体
if(a[1].first==a[1].second)
{
k=0;
res1=a[1].first,res2=a[1].second;
for(int i=1;i<=6;i+=2)
{
if(a[i].first==res1&&a[i].second==res2) k++;
}
if(k==3)
{
puts("POSSIBLE");
continue;
}
}
bool st=0;//判断长方体
if(a[1].first==a[3].first)
{
res1=a[1].second,res2=a[3].second;
if((a[5].first==res1&&a[5].second==res2)||(a[5].first==res2&&a[5].second==res1)) st=1;
else st=0;
}
if(st) puts("POSSIBLE");
else puts("IMPOSSIBLE");
}
}
return 0;
}
/*
2 3
2 4
2 5
2 3
3 4
3 5
1 2
1 3
1 4
1 1
1 6
1 5
5 2
4 3
3 4
1 1
3 6
6 5
*/
2*min_element(a,a+n) *max_element(a,a+n) 求数组的最小最大值
3.
for(int i=1;i<=n;i++)
{
ans[++cnt]=num;
int count=0;
for(int j=i;j<=n;j++)
{
f[a[j]]--;
//printf("j=%d f[%d]=%d\n",j,a[j],f[a[j]]);
if(f[a[j]]==0) num=min(a[j],num);
if(a[j]<ans[cnt]&&!st[a[j]]) count++,st[a[j]]=1;
if(count==ans[cnt])
{
//memset(st,0,sizeof st); 速度比下面慢
for(int k=0;k<ans[cnt];k++) st[k]=0;
i=j;//进入上面循环 i会自动加一
break;
}
}
}
for循环进入会自动加一 memset速度比遍历慢
4.erase用法5.
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
int find(int x)
{
return lower_bound(s,s+j,x)-s;
}
二分查找
6.struct 用法 及排序问题
bool cmp1(str a,str b)//不用加中括号
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
struct str{
int x,w,pos;
} a[N],b[N];
int t,m,n;
bool cmp1(str a,str b)
{
return a.w<b.w;
}
bool cmp2(str a,str b)
{
return a.x<b.x;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].w);
a[i].pos=i;
}
sort(a+1,a+m+1,cmp1);
long long ans=0;
for(int i=1;i<=2*n;i++)
{
ans+=a[i].w;
b[i].x=a[i].x;
b[i].w=a[i].w;
b[i].pos=a[i].pos;
}
sort(b+1,b+2*n+1,cmp2);
printf("%lld\n",ans);
for(int i=1;i<=n;i++)
{
printf("%d %d\n",b[i].pos,b[2*n+1-i].pos);
}
puts("");
}
return 0;
}