题目
思路
first
题目要求是有序有序整数对
所以可以用队列进行运算。为判断是否是从大到小进入队列,我们用
这段代码来判断 ——附c++ operator的用法汇总
bool operator<(const node&a) const {
return x>a.x||x==a.x&&y>a.y;
}
second
题目说明小明只踢第奇数个
判断石子的个数则用if语句来实现
if(i%2) {
} else{
}
third
题目最终要求的是最远踢出的距离
所以 ans=q.top().x+q.top().y;
代码
#define LOCAL
#include
#include
using namespace std;
struct node {
int x,y;
bool operator<(const node&a) const {
return x>a.x||x==a.x&&y>a.y;
}
};
priority_queue q;
priority_queue p;
int main() {
#ifdef LOCAL
freopen(“c:\BJZHC0\stone\stone1.in”, “r”, stdin);
freopen(“c:\BJZHC0\stone\stone1.out”, “w”, stdout);
#endif
int n,k=0,temp=0;
node t;
cin>>n;
for(int i=0; i<n; i++) {
cin>>t.x>>t.y;
q.push(t);
// p.push(t);
}
/*
while(!p.empty()) {
node n1;
n1.x=p.top().x;
n1.y=p.top().y;
cout<<n1.x<<" "<<n1.y<<endl;
p.pop();
}
*/
int i=1,ans=0;
node now;
while(!q.empty()) {
now.x=q.top().x+q.top().y;
now.y=q.top().y;
if(i%2) {
q.push(now);
ans=q.top().x+q.top().y;
q.pop();
} else
q.pop();
i++;
}
cout<<ans;
#ifdef LOCAL
fclose(stdin);//关闭重定向输入
fclose(stdout);//关闭重定向输出
#endif
return 0;
}