ตัวอย่าง Code STL
Jump to navigation
Jump to search
ข้อ Editor
โจทย์ : [1]
#include<stdio.h>
#include<string>
#include<list>
#include<iostream>
using namespace std;
list<string> ll;
list<string>::iterator itr = ll.begin();
list<string>::iterator it;
int main(){
char cmd[2];
string tmp;
int n;
scanf("%d",&n);
while(n--){
scanf("%s",cmd);
if(cmd[0]=='i'){
scanf("%s",&tmp[0]);
ll.insert(itr,tmp.c_str());
}else if(cmd[0]=='b'){
if(itr!=ll.begin()){
itr--;
itr=ll.erase(itr);
}
}else if(cmd[0]=='d'){
if(itr!=ll.end()) itr=ll.erase(itr);
}else if(cmd[0]=='l'){
if(itr!=ll.begin()) itr--;
}else if(cmd[0]=='r'){
if(itr!=ll.end()) itr++;
}
}
for(it=ll.begin(); it!=ll.end(); it++){
cout << *it << " " ;
}
printf("\n");
scanf(" ");
return 0;
}
ข้อ Pond
โจทย์ : [2]
#include<stdio.h>
#include<queue>
using namespace std;
int n,t,x;
priority_queue<int,vector<int>,greater<int> > h;
int main(){
scanf("%d%d",&n,&t);
while(n--){
scanf("%d",&x);
h.push(x);
}
while(t--){
printf("%d\n",h.top());
h.pop();
scanf("%d",&x);
h.push(x);
}
scanf(" ");
return 0;
}
ข้อ Trip
โจทย์ : [3] แบบที่ 1 ใช้ SET
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
struct ob{
int a,b,c;
bool operator<(const ob &x)const{
if(a<x.a) return true;
else if(a==x.a) return b>x.b;
else return false;
}
};
int n,a,b;
set<ob> x;
ob t;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
t.a=a; t.b=b; t.c=i;
x.insert(t);
}
for(set<ob>::iterator i=x.begin();i!=x.end();i++){
printf("%d\n",i->c);
}
scanf(" ");
return 0;
}
แบบที่ 2 ใช้ Vector
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct ob{
int a,b,c;
bool operator()(ob i,ob j){
if(i.a<j.a) return true;
else if(i.a==j.a) return i.b>j.b;
else return false;
}
};
int n,a,b;
vector<ob> x;
ob t;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
t.a=a; t.b=b; t.c=i;
x.push_back(t);
}
sort(x.begin(),x.end(),ob());
for(int i=0;i<n;i++){
printf("%d\n",x[i].c);
}
scanf(" ");
return 0;
}