text
stringlengths
49
983k
#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <tuple> using namespace std; using T = tuple<int, int, int>; // (rの合計, id, range) struct Object { int x, y; bool isGoal; }; const int INF = (1 << 28); int cost[200][1010]; int main() { int W, H, N; cin >> W >> H >> N; vector<Object> objects; for (int i = 0; i < N; i++) { int x, y; cin >> x >> y; objects.push_back((Object){x, y, false}); } objects.push_back((Object){1, 1, true}); objects.push_back((Object){W, H, true}); for (int i = 0; i < 200; i++) { for (int j = 0; j < 1010; j++) { cost[i][j] = INF; } } priority_queue<T, vector<T>, greater<T>> Q; Q.push(make_tuple(0, N, 0)); int ans = INF; while (!Q.empty()) { int rsum, id, range; tie(rsum, id, range) = Q.top(); Q.pop(); if (id == N + 1) { // goal ans = rsum; break; } for (int i = 0; i < objects.size(); i++) { Object& o = objects[i]; int d = abs(o.x - objects[id].x) + abs(o.y - objects[id].y); if (id == N) { // 自分がスタート if (not o.isGoal) { if (cost[i][d] > rsum + d) { cost[i][d] = rsum + d; Q.push(make_tuple(cost[i][d], i, d)); } } // else } else { if (not o.isGoal) { int r = max(0, d - 1 - range); if (cost[i][r] > rsum + r) { cost[i][r] = rsum + r; Q.push(make_tuple(cost[i][r], i, r)); } } else { // 相手がゴールかつ範囲内 int r = max(0, d - range); if (cost[i][r] > rsum + r) { cost[i][r] = rsum + r; Q.push(make_tuple(cost[i][r], i, r)); } } } } } if (ans >= INF) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<map> #include<cstring> #include<string> #include <math.h> #include<algorithm> // #include <boost/multiprecision/cpp_int.hpp> #include<functional> #define int long long #define inf 1000000007 #define pa pair<int,int> #define ll long long #define pal pair<double,double> #define ppap pair<pa,int> #define ssa pair<string,int> #define mp make_pair #define pb push_back #define EPS (1e-8) #define equals(a,b) (fabs((a)-(b))<EPS) int dx[4]={0,-1,0,1}; int dy[4]={1,0,-1,0}; using namespace std; class pa3{ public: int x,y,z; pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} bool operator < (const pa3 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; return z<p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa3 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; return z>p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa3 &p) const{ return x==p.x && y==p.y && z==p.z; } bool operator != (const pa3 &p) const{ return !( x==p.x && y==p.y && z==p.z); } }; class pa4{ public: double x; int y,z,w; pa4(double x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} bool operator < (const pa4 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; if(z!=p.z)return z<p.z; return w<p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa4 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; if(z!=p.z)return z>p.z; return w>p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa4 &p) const{ return x==p.x && y==p.y && z==p.z &&w==p.w; } }; class pa2{ public: int x,y; pa2(int x=0,int y=0):x(x),y(y) {} pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} pa2 operator * (int a) {return pa2(x*a,y*a);} bool operator < (const pa2 &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa2 &p) const{ return x != p.x ? x>p.x: y>p.y; } bool operator == (const pa2 &p) const{ return abs(x-p.x)==0 && abs(y-p.y)==0; } bool operator != (const pa2 &p) const{ return !(abs(x-p.x)==0 && abs(y-p.y)==0); } }; #define ppa pair<int,pas> class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } string itos( int i ) { ostringstream s ; s << i ; return s.str() ; } int gcd(int v,int b){ if(v>b) return gcd(b,v); if(v==b) return b; if(b%v==0) return v; return gcd(v,b%v); } double distans(double x1,double y1,double x2,double y2){ double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); return sqrt(rr); } /* int pr[1000010]; int inv[1000010]; int beki(int wa,int rr,int warukazu){ if(rr==0) return 1%warukazu; if(rr==1) return wa%warukazu; if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu; int zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } double bekid(double w,int r){ if(r==0) return 1.0; if(r==1) return w; if(r%2) return bekid(w,r-1)*w; double f=bekid(w,r/2); return f*f; } int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<=ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<=ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ //priority_queue<pa3,vector<pa3>,greater<pa3>> pq; //sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ //vector<double> ans(100000000),ans2(100000000) /* struct Segmin{ // 1 // 2 3 // 4 5 6 7 private: public: // (1<<15)=32768 // 1<<16 = 65536 // 1<<17 = 131072 // 1<<18 = 262144 int cor=(1<<17); vector<pa> vec; void shoki1(){ vec.resize(2*cor+3, mp(0,0)); for(int i=cor;i<2*cor;i++)vec[i].second=1; } void shoki2(){ for(int i=cor-1;i>0;i--) { if(vec[2*i].first<vec[2*i+1].first) vec[i]=vec[2*i]; else if(vec[2*i].first>vec[2*i+1].first)vec[i]=vec[2*i+1]; else vec[i]=mp(vec[2*i].first,vec[2*i].second+vec[2*i+1].second); } } void updadd(int x,int w){ //x ���ڂ� w���Z x+=cor; vec[x].first+=w; while(1){ x/=2; if(x==0) break; if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x]; else if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x+1]; else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second); } } void updchan(int x,int w){ //x���ڂ�w�ɕύX x+=cor; vec[x].first=w; while(1){ x/=2; if(x==0) break; if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x]; else if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x+1]; else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second); } } // [a,b) // k-th node // k no kukanha [l,r) pa segmin(int a,int b,int k=1,int l=0,int r=-10){ if(r<0)r=cor; // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl; if(a<=l && r<=b){ return vec[k]; } if(r<=a || b<=l){ return mp((1ll<<31)-1,-1); } pa v1=segmin(a,b,k*2,l,(l+r)/2),v2=segmin(a,b,k*2+1,(l+r)/2,r); if(v1.first<v2.first)return v1; else if(v1.first>v2.first) return v2; else return mp(v1.first,v1.second+v2.second); } }; struct Segmax{ // 1 // 2 3 // 4 5 6 7 private: public: // (1<<15)=32768 // 1<<16 = 65536 // 1<<17 = 131072 // 1<<18 = 262144 int cor=(1<<17); vector<pa> vec; void shoki1(){ vec.resize(2*cor+3, mp(0,0)); for(int i=cor;i<2*cor;i++)vec[i].second=1; } void shoki2(){ for(int i=cor-1;i>0;i--) { if(vec[2*i].first>vec[2*i+1].first) vec[i]=vec[2*i]; else if(vec[2*i].first<vec[2*i+1].first)vec[i]=vec[2*i+1]; else vec[i]=mp(vec[2*i].first,vec[2*i].second+vec[2*i+1].second); } } void updadd(int x,int w){ //x ���ڂ� w���Z x+=cor; vec[x].first+=w; while(1){ x/=2; if(x==0) break; if(vec[2*x].first>=vec[2*x+1].first) vec[x]=vec[2*x]; else vec[x]=vec[2*x+1]; } } void updchan(int x,int w){ //x���ڂ�w�ɕύX x+=cor; vec[x].first=w; while(1){ x/=2; if(x==0) break; if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x]; else if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x+1]; else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second); } } // [a,b) // k-th node // k no kukanha [l,r) pa segmax(int a,int b,int k=1,int l=0,int r=-10){ if(r<0)r=cor; // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl; if(a<=l && r<=b){ return vec[k]; } if(r<=a || b<=l){ return mp(-(1ll<<31),-1); } pa v1=segmax(a,b,k*2,l,(l+r)/2),v2=segmax(a,b,k*2+1,(l+r)/2,r); if(v1.first>v2.first)return v1; else if(v1.first<v2.first) return v2; else return mp(v1.first,v1.second+v2.second); } }; */ int w,h,n; priority_queue<pa3,vector<pa3>,greater<pa3>> pq; bool sumi[200][2000]={0}; vector<pa3> G[110][1010]; int x[110],y[110]; signed main(){ cin>>w>>h>>n; for(int i=1;i<=n;i++)cin>>x[i]>>y[i]; for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++){ int d=abs(x[i]-x[j])+abs(y[i]-y[j]); for(int k=0;k<d;k++){ G[i][k].pb((pa3){j,d-k-1,d-k-1}); G[j][k].pb((pa3){i,d-k-1,d-k-1}); } } for(int i=1;i<=n;i++){ for(int j=0;j<=1001;j++){ G[i][j].pb((pa3){i,j+1,1}); G[i][j+1].pb((pa3){i,j,0}); } int d=x[i]-1+y[i]-1; // cout<<i<<" "<<d<<endl; G[0][0].pb((pa3){i,max(0ll,d),max(0ll,d)}); d=w-x[i]+h-y[i]; // cout<<i<<" "<<d<<endl; G[i][max(0ll,d)].pb((pa3){0,1,0}); } pq.push((pa3){0,0,0}); while(pq.size()){ pa3 z=pq.top(); pq.pop(); if(sumi[z.y][z.z])continue; sumi[z.y][z.z]=1; // cout<<z.x<<" "<<z.y<<" "<<z.z<<endl; if(z.y==0 && z.z==1){ cout<<z.x<<endl; return 0; } for(auto v:G[z.y][z.z]){ pq.push((pa3){z.x+v.z,v.x,v.y}); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; #define fi first #define se second #define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<x<<endl #define mmax(x,y) (x>y?x:y) #define mmin(x,y) (x<y?x:y) #define maxch(x,y) x=mmax(x,y) #define minch(x,y) x=mmin(x,y) #define uni(x) x.erase(unique(all(x)),x.end()) #define exist(x,y) (find(all(x),y)!=x.end()) #define bcnt __builtin_popcount #define INF INT_MAX/3 #define mod 1000000007 struct state{ int d; P p; }; bool operator<(const state& a,const state& b){ return a.d > b.d; } int w,h,n; int x[101],y[101]; int dist[105][1001]; int main(){ scanf("%d%d%d",&w,&h,&n); rep(i,n)scanf("%d%d",x+i,y+i); priority_queue<state> que; rep(i,n+1)rep(j,w+h+1)dist[i][j]=INF; que.push((state){0,P(n,0)}); int res=INF; while(que.size()){ P p=que.top().p; int d=que.top().d; que.pop(); if(dist[p.fi][p.se]!=INF)continue; dist[p.fi][p.se]=d; if(p.fi==n){ // start rep(i,n){ int md=abs(x[i]-1)+abs(y[i]-1); que.push((state){d+md,P(i,md)}); } }else{ // light rep(i,n){ if(x[i]<x[p.fi]&&y[i]<y[p.fi])continue; if(i==p.fi)continue; int md=abs(x[i]-x[p.fi])+abs(y[i]-y[p.fi]); if(md<=p.se+1){ que.push((state){d,P(i,0)}); }else{ md-=p.se+1; que.push((state){d+md,P(i,md)}); } } { // -> end int md=abs(x[p.fi]-w)+abs(y[p.fi]-h); if(md<=p.se){ minch(res,d); }else{ md-=p.se; minch(res,d+md); } } } } cout<<res<<endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ ll dist[105][505*505]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll W,H,N; cin >> W >> H >> N; vector<ll> x(N+1),y(N+1); x[0] = y[0] = 1; for(int i = 1; i <= N;i++) cin >> x[i] >> y[i]; vector<vector<ll>> G(N+1); for(int i = 1; i <= N; i++){ for(int j = 1; j <= N;j++){ if(i == j) continue; //if(x[i] <= x[j]){// && y[i] <= y[j]){ G[i].push_back(j); //} } } for(int i = 0; i < 105;i++)for(int j = 0; j < 505*505; j++) dist[i][j] = LINF; typedef pair<ll,pll> plpll; // dist[0][0] = 0; priority_queue<plpll,vector<plpll>,greater<plpll>> pq; for(int i = 1; i <= N;i++){ ll cost = abs(x[i]-x[0])+abs(y[i]-y[0]); pq.push({cost,{i,cost}}); dist[i][cost] = cost; } // pq.push({0,{0,0}}); while(pq.size()){ auto p = pq.top(); pq.pop(); ll d = p.first, n = p.second.first, r = p.second.second; if(dist[n][r] < d) continue; for(ll next : G[n]){ ll mdis = abs(x[n]-x[next]) + abs(y[n]-y[next]); if(r >= mdis-1){ if(dist[next][0] > dist[n][r]){ dist[next][0] = dist[n][r]; pq.push({dist[next][0],{next,0}}); } }else{ ll cost = mdis-r-1; if(dist[next][cost] > dist[n][r] + cost){ dist[next][cost] = dist[n][r] + cost; pq.push({dist[next][cost],{next,cost}}); } } } } ll ans = LINF; for(int i = 1; i <= N;i++){ for(int j = 0; j < 505*505;j++){ if(dist[i][j]==LINF)continue; ll mdis = abs(W-x[i])+abs(H-y[i]); ans = min(ans,dist[i][j]+max(mdis-j,0LL)); } } cout << ans << endl; return 0; }
#include<iostream> #include<set> #include<vector> #include<algorithm> #include<math.h> using namespace std; struct Node { int now, cost, light; }; bool operator<(const Node &lhs, const Node &rhs) { return lhs.cost < rhs.cost; } int dist(int x, int y, int x2, int y2) { return abs(x - x2) + abs(y - y2); } int main () { int w, h, n; cin >> w >> h >> n; vector<pair<int, int>> xy(n); for(int i = 0; i < n; i++) cin >> xy[i].first >> xy[i].second; multiset<Node> q; q.insert(Node{0, 0, 0}); bool c[110][1000] = {false}; while(q.size() > 0) { Node now = *q.begin(); q.erase(q.begin()); if (c[now.now][now.light]) { continue; } c[now.now][now.light] = true; if (now.now == n + 1) { cout << now.cost << endl; return 0; } if (now.now == 0) { for(int i = 0; i < n; i++) { int cost = dist(1, 1, xy[i].first, xy[i].second); q.insert(Node{i + 1, cost, cost}); } } else { for(int i = 0; i < n; i++) { int cost = dist(xy[now.now - 1].first, xy[now.now - 1].second, xy[i].first, xy[i].second) - 1 - now.light; if (c[i + 1][max(0, cost)]) continue; q.insert(Node{i + 1, now.cost + max(0, cost), max(0, cost)}); } int cost = dist(xy[now.now - 1].first, xy[now.now - 1].second, w, h); q.insert(Node{n + 1, now.cost + max(0, cost - now.light), -1}); } } return 0; }
#include<bits/stdc++.h> using namespace std; using P = pair<int, int>; const int M = 1000000007; int main() { int w, h, n; cin >> w >> h >> n; vector<P> p(n + 2); for (int i = 0; i < n; ++i) { cin >> p[i].first >> p[i].second; } int S = n, T = n + 1; p[S] = P(1, 1); p[T] = P(w, h); priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> q; vector<vector<int>> dists(n + 2, vector<int>(1010, 2000)); dists[S][0] = 0; q.push(make_pair(0, P(S, 0))); while (!q.empty()) { pair<int, P> pp = q.top(); q.pop(); int c = pp.first; int i = pp.second.first; int r = pp.second.second; for (int j = 0; j < n; ++j) { if (i == j) continue; int nr = max(0, abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second) - r); if (i < n && nr > 0) { --nr; } if (dists[j][nr] > c + nr) { dists[j][nr] = c + nr; q.push(make_pair(c + nr, P(j, nr))); } } } vector<vector<int>> distt(n + 2, vector<int>(1010, 2000)); distt[T][0] = 0; q.push(make_pair(0, P(T, 0))); while (!q.empty()) { pair<int, P> pp = q.top(); q.pop(); int c = pp.first; int i = pp.second.first; int r = pp.second.second; for (int j = 0; j < n; ++j) { if (i == j) continue; int nr = max(0, abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second) - r); if (i < n && nr > 0) { --nr; } if (distt[j][nr] > c + nr) { distt[j][nr] = c + nr; q.push(make_pair(c + nr, P(j, nr))); } } } int ans = 2000; for (int i = 0; i < n; ++i) { for (int j = 0; j < 1010; ++j) { if (j > 0) { dists[i][j] = min(dists[i][j], dists[i][j - 1] + 1); distt[i][j] = min(distt[i][j], distt[i][j - 1] + 1); } ans = min(ans, dists[i][j] + max(0, abs(p[T].first - p[i].first) + abs(p[T].second - p[i].second) - j)); ans = min(ans, distt[i][j] + max(0, abs(p[S].first - p[i].first) + abs(p[S].second - p[i].second) - j)); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) continue; int d = abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second) - 1; for (int k = 0; k <= d; ++k) ans = min(ans, dists[i][k] + distt[j][d - k]); } } cout << ans << "\n"; return 0; }
#ifndef VS #include<bits/stdc++.h> #endif using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if(1) #else #define CIN_ONLY if(1) struct cww {cww() {CIN_ONLY{ios::sync_with_stdio(false); cin.tie(0);}} }star; #define DEBUG if(0) #endif #define ALL(v) (v).begin(),(v).end() #define REC(ret, ...) std::function<ret (__VA_ARGS__)> template <typename T>inline bool chmin(T &l, T r){bool a = l>r; if (a)l = r; return a;} template <typename T>inline bool chmax(T &l, T r){bool a = l<r; if (a)l = r; return a;} template <typename T>istream& operator>>(istream &is, vector<T> &v){for (auto &it : v)is >> it;return is;} class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }}; typedef pair<int, int> P; constexpr int L = 1123; constexpr int N = 112; int dist[N][L]; queue<P> que[L]; int r[N]; int c[N]; int R, C, n; int d(int i, int j) { return abs(r[i] - r[j]) + abs(c[i] - c[j]); } int main() { cin >> R >> C >> n; if (R == 1 && C == 1) { cout << 0 << endl; return 0; } for (int i : range(N))for (int j : range(L))dist[i][j] = L; for (int i : range(n)) { cin >> r[i] >> c[i]; int cost = r[i] + c[i] - 2; que[cost].push({ i,cost }); chmin(dist[i][cost], cost); } for (int cost : range(L)) { while (que[cost].size()) { int v, l; tie(v, l) = que[cost].front(); que[cost].pop(); if (cost > dist[v][l])continue; for (int u : range(n)) { int need = d(u, v) - l - 1; if(need <=0)need = 0; if (chmin(dist[u][need], cost + need)) { que[cost+need].push({ u,need }); } } if (cost + 1 < L&&l + 1 < L) { if (chmin(dist[v][l + 1], cost + 1)) { que[cost + 1].push({ v,l + 1 }); } } } } int ret = L; for (int i : range(n)) { for (int l : range(L)) { int cost = R - r[i] + C - c[i] - l; if (cost < 0)cost = 0; chmin(ret, dist[i][l] + cost); } } cout << ret << endl; return 0; }
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-2; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; int w, h, n; int x[100], y[100]; int d[100][1001]; struct vertice { int id, bl; int cost; bool operator<(const vertice &v)const { if (v.cost != cost)return cost > v.cost; if (v.id != id)return v.id > id; else return v.bl > bl; } }; priority_queue<vertice> q; void init() { rep(i, 100) { rep(j, 1001) { d[i][j] = mod; } } } int main() { init(); cin >> w >> h >> n; rep(i, n) { cin >> x[i] >> y[i]; } rep(i, n) { int z = x[i] - 1 + y[i] - 1; q.push({ i,z,z }); d[i][z] = z; } int ans = mod; //dijkstra while (!q.empty()) { vertice v = q.top(); q.pop(); int id = v.id,bl=v.bl,cost=v.cost; int dis = h - x[id] + w - y[id]; ans = min(ans, cost+max(0, dis - bl)); rep(i, n) { if (i == id)continue; int dis = abs(x[id] - x[i]) + abs(y[id] - y[i]); dis = max(0, dis - bl-1); if (d[i][dis] > cost + dis) { d[i][dis] = cost + dis; q.push({ i,dis,cost + dis }); } } } cout << ans << endl; //stop return 0; }
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) using namespace std; //kaewasuretyuui typedef long long ll; //#define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; #define mt make_tuple typedef tuple<int,int,int> tp; typedef vector<tp> vt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} //template<class C>constexpr int size(const C &c){return (int)c.size();} //template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;} const double PI=acos(-1); const double EPS=1e-7; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; typedef int Def; int n,m,w; vp in; class DIJ{ public: struct edge{ Def to,cost; }; vector<vector<edge> >G; vector<vector<Def> >d;//distance void dij(){ d=vector<vector<Def> >(w,vector<Def>(n+m+1,inf)); d[w-3][0]=d[w-2][0]=0;//now oktime ->time priority_queue<tp>q; q.push(tp(0,w-2,0));//time now v_0 n-1を通ったか q.push(tp(0,w-3,0));//time now v_0 n-1を通ったか while(!q.empty()){ Def cost,pos,t; tie(cost,pos,t)=q.top(); cost*=-1; q.pop(); if(cost>d[pos][t])continue; rep(i,w)if(i!=pos){ int j=abs(in[pos].first-in[i].first)+abs(in[pos].second-in[i].second)-1-t; j=max(0,j); // if(i==w-1&&j)continue; Def to=i; Def ncost=cost+j;//e.cost*v; Def nt=j; if(abs(in[pos].first-in[to].first)+abs(in[pos].second-in[to].second)-1 >t+nt)continue; if(ncost<d[to][nt]){ d[to][nt]=ncost; q.push(tp(-ncost,to,nt)); } } } Def out=inf; rep(i,n+m+1)out=min(out,d[w-1][i]); cout<<out<<endl; // cout<<d[w-1][0]<<endl; } }; int main(){ cin>>n>>m>>w; in=vp(w+3); rep(i,w)cin>>in[i].first>>in[i].second; in[w]={1,0}; in[w+1]={0,1}; in[w+2]={n,m+1}; w+=3; DIJ dij; dij.dij(); }
#include<bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> PP; int w,h,n; int x[101],y[101]; int dp[101][501]; int main(void){ scanf("%d%d%d",&w,&h,&n); for(int i=0;i<n;i++){ scanf("%d%d",&x[i],&y[i]); x[i]--; y[i]--; } priority_queue<PP,vector<PP>,greater<PP> > que; for(int i=0;i<n;i++){ for(int j=0;j<=500;j++){ dp[i][j]=50000; } } for(int i=0;i<n;i++){ int di=x[i]+y[i]; dp[i][di]=di; que.push(PP(di,P(i,di))); } while(que.size()){ PP p=que.top(); que.pop(); int v=p.second.first; int len=p.second.second; int cost=p.first; if(dp[v][len]<cost)continue; for(int i=0;i<n;i++){ int di=abs(x[i]-x[v])+abs(y[i]-y[v]); int nc=max(0,di-1-len); if(dp[i][nc]>dp[v][len]+nc){ dp[i][nc]=dp[v][len]+nc; que.push(PP(dp[i][nc],P(i,nc))); } } } int ans=50000; for(int i=0;i<n;i++){ int di=abs(w-1-x[i])+abs(h-1-y[i]); for(int j=0;j<=500;j++){ ans=min(ans,dp[i][j]+max(0,di-j)); } } printf("%d\n",ans); return 0; }
#include<algorithm> #include<iostream> #include<queue> #include<vector> using namespace std; typedef long long lint; typedef vector<int>vi; typedef pair<int,int>pii; typedef pair<lint,pii>plpii; #define rep(i,n)for(int i=0;i<(int)(n);++i) const int N=1010; const lint inf=1e16; int main(){ int w,h,n; cin>>w>>h>>n; int mi=1e8; vector<pii> pts(n+2); pts[0]=pii(1,1); pts[1]=pii(w,h); rep(i,n){ int x,y; cin>>x>>y; pts[i+2]=pii(x,y); } priority_queue<plpii,vector<plpii>,greater<plpii> > que; vector<vector<lint> > dis(n+2,vector<lint>(N,inf)); rep(i,n){ int d=abs(pts[i+2].first-1)+abs(pts[i+2].second-1); que.push(plpii(d,pii(i+2,d))); } while(!que.empty()){ plpii top=que.top();que.pop(); lint d=top.first; pii xy=top.second; int x=xy.first,y=xy.second; if(dis[x][y]<=d)continue; dis[x][y]=d; rep(i,n+2){ if(i==x||i==0)continue; int c=abs(pts[i].first-pts[x].first)+abs(pts[i].second-pts[x].second); c=max(0,i==1?c-y:(c-y-1)); if(c>=N)continue; int nx=i; int ny=i==1?0:c; if(dis[nx][ny]<=d+c)continue; dis[nx][ny]=d+c+1; que.push(plpii(d+c,pii(nx,ny))); } } cout<<dis[1][0]<<endl; }
#include<set> #include<iostream> using namespace std; int main() { int n; set<string> a; string cmd, str; cin>>n; for(int i = 0; i < n; i++) { cin>>cmd>>str; if(cmd.compare("insert") == 0) a.insert(str); else { if(a.find(str) != a.end()) cout<<"yes"<<endl; else cout<<"no"<<endl; } } }
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; int main() { int n; char str[10], com[13]; map<string, bool> T; // 標準ライブラリからmapを使用 cin >> n; for ( int i = 0; i < n; i++ ) { scanf("%s%s", com, str); if ( com[0] == 'i' ) T[string(str)] = true; else { if ( T[string(str)] ) printf("yes\n"); else printf("no\n"); } } return 0; }
#include <iostream> #include <cstdio> #include <string> #include <set> using namespace std; #define rep(x,to) for(int x=0; x<(to); (x)++) int main() { int n; char s[5]; string t; set<string> Set; cin >> n; rep(i,n) { scanf("%s", s); if(s[0] == 'i') { cin >> t; Set.insert(t); } else { cin >> t; if(Set.find(t) == Set.end()) printf("no\n"); else printf("yes\n"); } } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; int main(){ string tmp; map <string,bool> a; int n,i; cin>>n; for(i=0;i<n;i++){ cin>>tmp; if(tmp=="insert"){ cin>>tmp; a.insert(make_pair(tmp,true)); } else { cin>>tmp; if(a.count(tmp)) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include<set> #include<iostream> #include<string> using namespace std; int main () { set<string> dictionary; string command; string word; int n,i; cin>>n; for(i=0;i<n;i++){ if(cin>>command&&command=="insert"){ cin>>word; dictionary.insert(word); } else{ cin>>word; if(dictionary.count(word)){ cout<<"yes"<<endl; } else { cout<<"no"<<endl; } } } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <set> using namespace std; int main() { set<string> st; int n; cin>>n; while (n--) { char cmd[8], str[20]; scanf("%s %s",cmd,str); if (!strcmp(cmd,"insert")) { st.insert(str); } else { cout<<(st.find(str)!=st.end()?"yes":"no")<<endl; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <unordered_set> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; unordered_set<string> d; while (n--) { string cmd, s; cin >> cmd >> s; if (cmd == "insert") d.insert(s); else cout << (d.find(s) != d.end() ? "yes" : "no") << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; set<string> s; int main(){ char i[5]; char o[13]; int n; cin>>n; while(n--){ cin>>i; cin>>o; string str; if(i[0]=='i'){ s.insert(o); } else if(i[0]=='f'){ if(s.count(o)!=0){ cout<<"yes"<<endl; }else{ cout<<"no"<<endl; } } } return 0; }
#include<map> #include<iostream> #include<string> using namespace std; int main(){ int n; cin>>n; map<string, bool> mp; for(int i=0;i<n;i++){ string s1, s2; cin>>s1>>s2; if(s1 == "insert"){ mp[s2] = true; }else{ if(mp[s2]){ cout<<"yes"<<endl; }else{ cout<<"no"<<endl; } } } return 0; }
#include<iostream> #include<set> using namespace std; int main(){ set<string> st; int n; cin >> n; for (int i = 0; i < n; i++){ string act; string str; cin >> act >> str; if (act == "insert"){ st.insert(str); }else if (act == "find"){ set<string>::iterator itr = st.find(str); if (itr == st.end()){ cout << "no" << endl; } else { cout << "yes" << endl; } } } return 0; }
#include <iostream> #include <string> #include <set> using namespace std; int main(void) { ios_base::sync_with_stdio(false); int n; string o,s; set<string> set; cin >> n; for(int i=0;i<n;i++){ cin >> o >> s; if(o=="insert"){ set.insert(s); } else{ if(set.find(s)!=set.end()){ cout << "yes" << endl; } else cout << "no" << endl; } } return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0; i<n; i++) using namespace std; int main(){ int n; char str[10],com[13]; map<string,bool> mp; cin >> n; rep(i,n){ cin >> com >> str; if(com[0] == 'i') mp[string(str)] = true; else { if(mp[string(str)]) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n;string s, p; cin >> n; map<string, bool> T; for(int i = 0; i < n; i++){ cin >> s >> p; if(s == "insert") T[p] = true; else cout << (T[p] ? "yes" : "no") << endl; } return 0; }
#include<iostream> #include<set> #include<string> using namespace std; int main(){ ios::sync_with_stdio(false); int n,i; cin >> n; string command, str; set<string> S; for (i=0;i<n;i++){ cin >> command >> str; if (command=="insert"){ S.insert(str); }else{ if (S.find(str)==S.end()){ cout << "no" <<endl; }else{ cout <<"yes" << endl; } } } return 0; }
#include <set> #include <string> #include <iostream> using namespace std; int main() { int n; string a,b; typedef set<string> set_t; set_t A; cin >> n; for (int i=1; i<=n; i++){ cin >> a >> b; if (a == "insert"){ A.insert(b); }else{ cout << (A.count(b) ? "yes" : "no") << endl; } } }
#include<bits/stdc++.h> using namespace std; int main(){ int i,n; cin>>n; string order,word; unordered_set<string> d; for(i=0;i<n;i++){ cin>>order>>word; if(order[0]=='i'){ d.insert(word); }else{ if(d.count(word)==0){ printf("no\n"); }else{ printf("yes\n"); } } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; char c[20]; string s; map<string,bool>A; for(int i=0;i<n;i++){ cin>>c; if(c[0]=='i'){ cin>>s; A[string(s)]=true; } else if(c[0]=='f'){ cin>>s; if(A[string(s)]){ cout <<"yes"<<endl; } else{ cout << "no"<<endl; } } } return 0; }
#include<string> #include<iostream> #include<algorithm> #include<map> using namespace std; int main(){ std::ios_base::sync_with_stdio(false); map<string,bool> mp; int n; cin >> n; while(n--){ string BUFF,c; cin >> BUFF >> c; if(BUFF=="insert") mp[c] = true; else cout << (mp[c]?"yes":"no") << endl; } }
#include<iostream> #include<vector> #include<string> #include<set> using namespace std; int main() { int a; cin >> a; set<string>b; for (int c = 0; c < a; c++) { string d, e; cin >> d >> e; if (d == "insert")b.insert(e); else { if (b.find(e) == b.end())cout << "no" << endl; else cout << "yes" << endl; } } }
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; set<string> str; for(int i=0; i<n; i++){ string command; cin >> command; string s; cin >> s; if(command=="insert"){ str.insert(s); } else{ if(str.find(s) != str.end()){ cout << "yes" << endl; } else cout << "no" << endl; } } return 0; }
#include <iostream> #include <set> using namespace std; int main() { int n = 0; cin >> n; set<string> dic; for (int i = 0; i < n; i++) { string buf1, buf2; cin >> buf1 >> buf2; if (buf1 == "insert") { dic.insert(buf2); } else { if (dic.find(buf2) != dic.end()) { cout << "yes" << '\n'; } else { cout << "no" << '\n'; } } } return 0; }
#include <iostream> #include <cstdio> #include <map> #include <string> using namespace std; int main(void){ map<string, bool> dict; int n; cin >> n; for (int i = 0; i < n; i++) { char com[13], str[10]; scanf("%s %s", com, str); if (com[0] == 'i') { dict[string(str)] = true; } else { if (dict[string(str)]) printf("yes\n"); else printf("no\n"); } } return 0; }
#include <stdio.h> #include <iostream> #include <string> #include <set> int main(void) { int i, j, k, n; scanf("%d", &n); std::set<std::string> se; std::string s; for(i = 0; i < n; ++i) { std::cin >> s; if(s[0] == 'i') { std::cin >> s; se.insert(s); } else { std::cin >> s; if(se.find(s) != se.end()) printf("yes\n"); else printf("no\n"); } } return 0; }
#include<iostream> #include<cstring> #include<map> using namespace std; int main(){ int n; string s,ss; map<string, bool>T; cin>>n; for(int i=0;i<n;i++){ cin>>s>>ss; if(s[0]=='i') T[ss]=1; else { if(T[ss]) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include<cstdio> #include<map> #include<string> using namespace std; int main() { int n; char str[10],com[13]; map<string,bool> T; cin>>n; for(int i=0;i<n;i++){ scanf("%s%s",com,str); if(com[0]=='i') T[string(str)]=true; else{ if(T[string(str)]) printf("yes\n"); else printf("no\n"); } } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ map<string,int>mp; int n; cin>>n; string str,str1; for(int i=0;i<n;i++){ cin>>str>>str1; if(str=="insert"){ mp[str1]=1; } else{ if(mp.count(str1)) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include <set> #include <string> using namespace std; string bufo,bufstr; int main() { int n; set<string> dict; cin >> n; for (int i=0;i<n;++i) { cin >> bufo >> bufstr; if (bufo=="insert") { dict.insert(bufstr); } else { int yn; yn = dict.count(bufstr); if (yn==1) cout << "yes" << endl; else cout << "no" << endl; } } }
#include<iostream> #include<cstdio> #include<map> #include<string> using namespace std; map<string,bool>S; char com[7]; char str[13]; int n; int main(){ scanf("%d",&n); for(int j=0;j<n;j++){ scanf("%s %s",com,str); if(com[0]=='i'){ S[string(str)]=true; } else if(S[string(str)]==true){ cout<<"yes"<<endl; } else if(S[string(str)]!=true){ cout<<"no"<<endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { set<string> s; int n; scanf("%d",&n); for(int i=0;i<n;i++) { string a,b; cin>>a>>b; if(a[0]=='i') s.insert(b); else if(a[0]='f') if(s.count(b)) printf("yes\n"); else printf("no\n"); } return 0; }
#include<iostream> #include<set> #include<cstdio> #include<cstring> using namespace std; int main(){ int n; string s1,s2; set<string>st; cin >> n; for(int i=0;i<n;i++){ cin >> s1 >> s2; if(s1 == "insert"){ st.insert(s2); } else { if(st.find(s2) != st.end()){ cout << "yes" << endl; } else cout << "no" << endl; } } return 0; }
#include <iostream> #include <cstdio> #include <set> using namespace std; int main(void){ char s[13]; char b[7]; std::set<string> m; int a; scanf("%d",&a); while(a--){ scanf("%s %s",b,s); if(b[0]=='i')m.insert(s); else{ if(m.find(s)!=m.end())cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include <string> #include <set> using namespace std; int main(){ int n; cin >> n; set<string> dict; for(int i=0;i<n;i++){ string command, word; cin >> command; if(command=="insert"){ cin >> word; dict.insert(word); }else{ cin >> word; if(dict.count(word)>0) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <iostream> #include <set> #include <string> using namespace std; int main() { set<string> s; int n; char ord[10], moji[15]; scanf("%d", &n); while (n--) { scanf("%s %s", ord, moji); if (ord[0] == 'i') s.insert(moji); else printf(s.find(moji) != s.end() ? "yes\n" : "no\n"); } }
#include <iostream> #include <set> using namespace std; int main(){ typedef set<string> set_t; set_t Dic; int n; cin >> n; string cmd, word; for(int i = 0; i < n; i++){ cin >> cmd; if(cmd == "insert"){ cin >> word; Dic.insert(word); } if(cmd == "find"){ cin >> word; if (Dic.count(word)){ cout << "yes" << endl; }else{ cout << "no" << endl; } } } return 0; }
#include <iostream> #include <set> using namespace std; int main() { int n = 0; cin >> n; set<string> dic; for (int i = 0; i < n; i++) { string buf1, buf2; cin >> buf1 >> buf2; if (buf1 == "insert") { dic.insert(buf2); } else { if (dic.find(buf2) != dic.end()) { cout << "yes" << endl; } else { cout << "no" << endl; } } } }
#include <iostream> #include <set> #include <string> using namespace std; int main(void) { int n; cin >> n; string order; string str; typedef set<string> set_t; set_t A; while(n--) { cin >> order; if (order == "insert") { cin >> str; A.insert(str); } else if (order == "find") { cin >> str; if (A.count(str) == 0) cout << "no" << endl; else cout << "yes" << endl; } } }
#include <iostream> #include <set> #include <string> using namespace std; int main() { typedef set<string> set_t; set_t A; string S,T; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> S >> T; if (S == "insert") { A.insert(T); } else { if (A.count(T)) { cout << "yes" << endl; } else { cout << "no" << endl; } } } return 0; }
#include<iostream> #include<map> #include<string> using namespace std; map<string,bool> dic; int main(){ int n;string opt,c; cin>>n; while(n--){ cin>>opt>>c; if(opt=="insert"){ dic[c]=true; }else if(opt=="find"){ if(dic.find(c)==dic.end()){ cout<<"no"<<endl; }else cout<<"yes"<<endl; } } return 0; }
#include <iostream> #include <string> #include <set> using namespace std; int main(void){ string str; int n; set<string> s; cin>>n; while(n--){ cin>>str; if(str=="insert"){ cin>>str; s.insert(str); }else{ cin>>str; set<string>::iterator it; it=s.find(str); if(it==s.end()) cout<<"no"<<endl; else cout<<"yes"<<endl; } } return 0; }
#include <iostream> #include <set> #include <algorithm> using namespace std; int main() { set<string> st; int n; string op, str; cin >> n; for(int i = 0; i < n; ++i) { cin >> op >> str; if(op == "insert") { // v.push_back(str); st.insert(str); } else { // find if(st.find(str) != st.end()) { cout << "yes" << endl; } else { cout << "no" << endl; } } } return 0; }
#include <set> #include <iostream> using namespace std; int main() { int n; cin >> n; string op,des; set<string> v; while(n--) { cin >> op >> des; if(op=="insert") { v.insert(des); } else { if(v.count(des)!=0) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include<iostream> #include<map> #include<string> using namespace std; int main(){ int N; map<string, bool> T; cin >> N; for (int i = 0; i < N; i++) { string s,t; cin>>s>>t; if (s == "insert") { T[t] = true; }else{ if (T[t]) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ cin.tie(0); ios::sync_with_stdio(false); set<string> dic; int n; cin>>n; string com,key; while(n--){ cin>>com>>key; if(com=="insert") dic.insert(key); else{ if(dic.find(key)==dic.end()) cout<<"no"<<endl; else cout<<"yes"<<endl; } } return 0; }
#include <iostream> #include <map> using namespace std; int main() { map<string, bool> exis; int n; cin>>n; for(int i = 0; i < n; ++i) { string opt, str; cin>>opt>>str; if(opt[0] == 'i') { exis[str] = true; } else { cout<<(exis.find(str) != exis.end()?"yes":"no")<<endl; } } return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <unordered_set> using namespace std; int main() { int N; cin >> N; unordered_set<string> histo; while(N--) { string s, t; cin >> s >> t; if(s == "insert") { histo.insert(t); } else { cout << (histo.count(t) ? "yes" : "no") << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; string str; set<string> dic; cin >> n; for (int i = 0; i < n; i++) { cin >> str; if (str == "insert") { cin >> str; dic.insert(str); } else if (str == "find") { cin >> str; cout << (dic.find(str) != dic.end() ? "yes" : "no") << endl; } } return 0; }
#include <iostream> #include <cstdio> #include <set> using namespace std; int main(){ ios::sync_with_stdio(false); int n; cin >> n; set<string> s; for(int i=0; i<n; i++){ string s1,s2; cin >> s1 >> s2; if(s1[0] == 'i') s.insert(s2); else{ if(s.find(s2) == s.end()) printf("no\n"); else printf("yes\n"); } } }
#include <bits/stdc++.h> using namespace std; #define AC return 0 typedef long long ll; map<string, bool> dic; int main() { int t; string od, ch; scanf("%d", &t); while (t--) { cin >> od >> ch; if (od[0] == 'i') dic[ch] = true; else { if (dic[ch]) printf("yes\n"); else printf("no\n"); } } }
#if 0 #endif #include <bits/stdc++.h> using namespace std; typedef long long int ll; int N; set<string> S; int main() { cin >> N; string s; for (int i = 0; i < N; i++) { cin >> s; if (s == "insert") { cin >> s; S.insert(s); } else { cin >> s; cout << (S.count(s) ? "yes" : "no") << endl; } } }
#include<bits/stdc++.h> using namespace std; #define int long long map<string,int> mp; signed main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; for(int i = 0; i < n; i++){ string s,t; cin >> s >> t; if(s == "insert") mp[t]++; if(s == "find"){ if(mp.find(t) != mp.end()) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; string q, s; set<string> st; cin >> n; while (n--) { cin >> q >> s; if (q[0] == 'i') st.insert(s); else if (st.count(s)) cout << "yes\n"; else cout << "no\n"; } }
#include <iostream> #include <map> #include <cstdio> using namespace std; int main(){ int n;scanf("%d", &n); char s[7], str[13]; map<string, int> m; for(int i = 0;i < n;i++){ scanf("%s %s", s, str); if(s[0] == 'i')m[str] = 1; else puts(m[str] ? "yes" : "no"); } return 0; } //1.78
#include<bits/stdc++.h> using namespace std; int main(void){ int n; string command, str; cin >> n; set<string> dic; for(int i = 0; i < n; i++){ cin >> command >> str; if(command == "insert"){ dic.insert(str); }else{ if(dic.find(str) != dic.end())cout << "yes" << endl; else cout << "no" << endl; } } }
#include <iostream> #include <set> using namespace std; int main() { set<string> s; int n; string order, str; cin >> n; for (int i = 0; i < n; i++) { cin >> order >> str; if (order == "insert") s.insert(str); else if (s.find(str) != s.end()) printf("yes\n"); else printf("no\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ set<string> acgt; int n; scanf("%d",&n); for(int i = 0; i < n; i++){ char op[10], str[20]; scanf("%s%s",op,str); if(op[0] == 'f'){ if(acgt.find(str) != acgt.end()) printf("yes\n"); else printf("no\n"); }else acgt.insert(str); } }
#include<iostream> #include<string> #include<set> using namespace std; int main() { set<string> d; int n, a; string o, b; cin >> n; for (a = 1; a <= n; a++) { cin >> o >> b; if (o == "insert") { d.insert(b); } else if (d.find(b) != d.end()) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { string com, str; int n; set<string> s; cin >> n; while (n--) { cin >> com >> str; if (com == "insert") s.insert(str); else if (com == "find") { if (s.find(str) != s.end()) cout << "yes" << endl; else cout << "no" << endl; } } }
#include<iostream> #include<set> #include<string> using namespace std; int main(){ set<string> S; string command,key; int n; cin>>n; for(int i=0;i<n;++i){ cin>>command; if(command=="insert"){ cin >> key; S.insert(key); } else if(command == "find"){ cin >> key; if(S.count(key) == 1){ cout << "yes" << endl; } else{ cout << "no" << endl; } } } }
#include <iostream> #include <set> #include <string> using namespace std; int main() { int n; string s, t; set<string> S; cin>>n; for (int i=0; i<n; i++) { cin>>s>>t; if (s=="insert") S.insert(t); if (s=="find") cout<<(S.count(t) ? "yes\n" : "no\n"); } return 0; }
#include<iostream> #include<set> using namespace std; int main() { typedef set<string> set_t; int n; set_t Dic; string cmd,word; cin >> n; for (int i = 0; i < n; i++) { cin >> cmd; if (cmd == "insert") { cin >> word; Dic.insert(word); } if (cmd == "find") { cin >> word; if (Dic.count(word)) { cout << "yes" << endl; } else cout << "no" << endl; } } }
#include<iostream> #include<map> #include<cstdio> using namespace std; int main(void) { map<string, int>m; int n; cin >> n; for (int i = 0; i < n; i++) { char s[20],s2[20]; scanf("%s", s); scanf("%s", s2); if (s[0] == 'i') { m[s2] = 1; } else { if (m[s2]) cout << "yes" << endl; else { cout << "no"<<endl; } } } return 0; }
#include <iostream> #include <bits/stdc++.h> #include <math.h> using namespace std; int main(){ int n; cin>>n; map<string,bool> dic; for(int i=0;i<n;i++){ string m,s; cin>>m>>s; if(m=="insert") dic[s]=true; else if(m=="find"){ if(dic.count(s)) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include<stdio.h> #include<string> #include<math.h> #include<algorithm> #include<iostream> #include<map> using namespace std; int main() { int n; string a,b; map<string,int> t; cin>>n; while(n--) { cin>>a>>b; if(a[0]=='i') t[b]=1; else { if(t[b]) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; int main(void){ int n; string s,t; map<string,int> msi; cin>>n; for(int i=0;i<n;i++){ cin>>s>>t; if(s=="insert"){ msi[t]=1; } else{ if(msi.find(t)==msi.end()) cout<<"no"<<endl; else cout<<"yes"<<endl; } } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; int main(){ int n; cin >> n; map<string,int> mp; for(int i = 0; i < n; ++i){ string s,t; cin >> s >> t; if(s == "insert") mp[t]++; if(s == "find"){ if(mp[t] == 0) puts("no"); else puts("yes"); } } return 0; }
#include <iostream> #include <string> #include <map> using namespace std; int n; string ins, s; int main() { cin >> n; map<string, bool> dict; while (n--) { cin >> ins >> s; if (ins[0] == 'i') { //insert dict[s] = true; } else { cout << (dict.count(s) ? "yes" : "no") << endl; } } return 0; }
#include <iostream> #include <map> #include <cstring> using namespace std; map <string,bool> dict; int main() { int n; cin >> n; for (int i=0;i<n;i++) { string a,b; cin >> a >> b; if (a=="insert") dict[b]=true; else { if (dict.find(b)!=dict.end()) cout << "yes\n"; else cout << "no\n"; } } return 0; }
#include <iostream> #include <map> using namespace std; int main(){ int n;cin >> n; string s, str; map<string, int> m; for(int i = 0;i < n;i++){ cin >> s >> str; if(s[0] == 'i'){ m[str] = 1; }else{ if(m[str])cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; map<string, int>M; while(t--){ string o, v; cin >> o >> v; if(o == "insert"){ M[v]++; }else if(o == "find"){ if(M[v]){ cout << "yes" << endl; }else{ cout << "no" << endl; } } } }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<n;i++) using namespace std; char t[20], s[20]; int main() { int n; scanf("%d", &n); unordered_set<string>se; rep(i, n) { scanf("%s%s", t, s); if (!strcmp(t, "insert"))se.insert(s); else puts(se.find(s) != se.end() ? "yes" : "no"); } }
#include "bits/stdc++.h" using namespace std; int main() { int n; scanf("%d", &n); map<string, bool> dic; char query[6], value[12]; for (int i = 0; i < n; i++) { scanf("%s %s", query, value); if (query[0] == 'i') { dic[value] = true; } else { printf(dic[value] ? "yes\n" : "no\n"); } } }
#include<cstdio> #include<map> #include<string> #include<iostream> using namespace std; int main() { int n; char str[10], com[13]; map<string, bool> T; cin >> n; for (int i = 0; i < n; i++) { scanf("%s%s", com, str); if (com[0] == 'i')T[string(str)] = true; else { if (T[string(str)]) printf("yes\n"); else printf("no\n"); } } return 0; }
#include <iostream> #include <vector> #include <queue> #include <map> using namespace std; int main(void) { int n; cin >> n; string ope,temp; map<string,bool> dict; for (int i = 0; i < n; ++i) { cin >> ope >> temp; if (ope == "insert") { dict[temp] = true; } else { if (dict[temp]) { cout << "yes" << endl; } else { cout << "no" << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; string s1, s2; scanf("%d", &n); map<string, int> m; for(int i = 0; i < n; i++) { cin >> s1 >> s2; if(s1 == "insert") m[s2] = 1; else if(s1 == "find") { if(m[s2]) printf("yes\n"); else printf("no\n"); } } return 0; }
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; int main() { int n; char str[10], com[13]; map<string, bool> T; cin >> n; for (int i = 0; i < n; i ++) { scanf("%s%s", com, str); if (com[0] == 'i') T[string(str)] = true; else { if (T[string(str)]) printf("yes\n"); else printf("no\n"); } } }
#include <iostream> #include <string> #include <set> using namespace std; int main(){ int n; cin>>n; set<string> st; while(n--){ string o,s; cin>>o>>s; if(o=="insert") st.insert(s); else{ if(st.find(s)!=st.end()) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include <set> int main() { int n; std::cin >> n; std::string command, str; std::set<std::string> dic; for (int i = 0; i < n; i++) { std::cin >> command >> str; if (command == "insert") { dic.insert(str); } else if (command == "find") { if (dic.count(str) == 0) { std::cout << "no\n"; } else { std::cout << "yes\n"; } } } return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0; i<n; i++) using namespace std; int main(){ int n; char str[10],com[13]; map<string,bool> mp; cin >> n; rep(i,n){ cin >> com >> str; if(com[0] == 'i') mp[str] = true; //cout << string(str) << endl;} else { if(mp[string(str)]) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string q, word; set<string> dic; for(int i=0; i < n; ++i){ cin >> q >> word; if(q=="insert"){ dic.insert(word); }else{ if(dic.count(word)){ cout << "yes" << endl; }else{ cout << "no" << endl; } } } }
#include <iostream> #include <set> #include <string> using namespace std; int main() { int N; cin >> N; string cmd, word; set<string> S; for (int i = 0; i < N; ++i) { cin >> cmd >> word; if (cmd == "insert") { S.insert(word); } else if (cmd == "find") { cout << (S.find(word) == S.end() ? "no" : "yes") << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<string, int> mp; for (int i = 0; i < n; i++) { char op[20], s[20]; scanf("%s %s", op, s); if (!strcmp(op, "find")) { cout << (mp[s] ? "yes" : "no") << endl; } else mp[s]++; } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; int main(){ int n; map<string, bool> mp; cin >> n; for(int i = 0; i < n; i++){ string s1, s2; cin >> s1 >> s2; if(s1 == "insert") mp[(s2)] = true; else{ if(mp[(s2)]) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <map> #include <string> #include <iostream> using namespace std; int main() { map<string,int>t; int n; cin>>n; string a,b; while(n--){ cin>>a>>b; if(a=="insert"){ t[b]=1; }else{ if(t[b]==0){ cout<<"no"<<endl; }else{ cout<<"yes"<<endl; } } } }
#if 0 #endif #include <iostream> #include <set> #include <string> using namespace std; int main(){ set<string> S; string cmd; string word; int n; cin>>n; for(int i=0;i<n;++i){ cin>>cmd; if(cmd=="insert"){ cin>>word; S.insert(word); }else if(cmd=="find"){ cin>>word; if(S.count(word)==1) cout<<"yes"<<endl; else cout<<"no"<<endl; } } }
#include<iostream> #include<set> #include<string> using namespace std; int main(){ int n; cin >> n; set<string>s; for(int i=0;i<n;i++){ string x,y; cin>>x>>y; if(x[0]=='i'){ s.insert(y); } else{ if(s.find(y)!=s.end())cout<<"yes\n"; else cout<<"no\n"; } } }
#include "bits/stdc++.h" using namespace std; #define ul unsigned long long #define ll long long int main() { ll n; string os,ks; cin>>n; map<string,int> m; for (ll i = 0; i < n; i++) { cin>>os; cin>>ks; if (os=="insert") { m[ks]=1; } else if (os== "find") { if (m[ks]) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }
#include <iostream> #include <string> #include <map> #include <cstdio> using namespace std; int main(){ map<string,bool> T; int n; char str[13],c[10]; cin>>n; while(n--){ scanf("%s%s",c,str); if(c[0]=='i') T[string(str)] = true; else{ if(T[string(str)]) printf("yes\n"); else printf("no\n"); } } return 0; }
#include <iostream> #include <set> using namespace std; int n; set<string> ss; string s,str; int main(){ ios::sync_with_stdio(false); cin >> n; while(n--){ cin >> str; if(str[0] == 'i'){ cin >> s; ss.insert(s); }else{ cin >> s; if(ss.find(s) == ss.end()){ cout << "no" << endl; }else{ cout << "yes" << endl; } } } return 0; }
#include<iostream> #include<string> #include<set> using namespace std; int main(){ string str; string command; set<string>st; int n; cin>>n; for(int i=0;i<n;i++){ cin>>command; cin>>str; if(command=="insert"){ st.insert(str); }else if(command=="find"){ if(st.find(str)==st.end()){ cout<<"no"<<endl; }else{ cout<<"yes"<<endl; } } } return(0); }