2474 Process scheduling a题 multiset multimap用法
#include <cstdio> #include <algorithm> #include <set> #include <map> using namespace std; const int maxn=50000+123; struct Proc{ int a[10], r[10]; bool rls; }p[maxn]; //int all[10][maxn], res[10][maxn]; int avl[10]; int m; typedef pair<int, int> pii; #define mp make_pair #define sc second #define fs first multiset< pii >ms2; multiset<int>ms; multimap< pii, int >mm2; multimap<int, int>mm; bool cmp(Proc x, Proc y) { for (int i=0; i<m; ++i) { if(x.r[i]<y.r[i])return true; if(x.r[i]>y.r[i])return false; } return true; } //int bin(int l, int r, int x, int pos) //{ // int mid; // while (l<r) // { // mid=(l+r)/2; // ///printf("%d %d %d %d\n", r, l, mid, x); // if(p[mid].rls)mid++; // if(p[mid].r[pos]<x)l=mid+1; // else if(p[mid].r[pos]>x)r=mid-1; // else return mid; // } // return r; //} int main() { int n; while (~scanf("%d%d", &n, &m)) { for (int i=0; i<m; ++i) { for (int j=0; j<n; ++j) { scanf("%d", &p[j].a[i]); p[j].rls=false; } } for (int i=0; i<m; ++i) for (int j=0; j<n; ++j) scanf("%d", &p[j].r[i]); for (int i=0; i<m; ++i) scanf("%d", avl+i); bool flag=true; sort(p, p+n, cmp); // for (int i=0; i<n; ++i) // { // for (int j=0; j<m; ++j) // { // printf("%d ", p[i].r[j]); // } // puts(""); // } if(m==1) { for(int i=0; i<n; ++i) { if(avl[0]>=p[i].r[0]) { avl[0]+=p[i].a[0]; p[i].rls=true; } else break; } }else if(m==3) { ms2.clear(); mm2.clear(); for (int i=0, j=0; i<n; ++i) { while (j<n && p[j].r[0]<=avl[0]) { pii tmp=mp(p[j].r[1], p[j].r[2]); ms2.insert(tmp); mm2.insert(mp(tmp, j)); j++; } multiset<pii>::iterator it=ms2.upper_bound(mp(avl[1], avl[2])), it1; ///if(it!=ms2.end())printf("%d %d\n", it->fs, it->sc); if(it!=ms2.begin()) {///while while(it!=ms2.begin()){ it1=it; it--; if(it->fs<=avl[1] && it->sc<=avl[2]) { multimap<pii, int>::iterator itlow; itlow=mm2.lower_bound(*it); ///itup=mm2.upper_bound(*it); int pos=itlow->sc; ///printf("%d %d\n", pos, i); p[pos].rls=true; for (int i=0; i<m; ++i) { avl[i]+=p[pos].a[i]; } ms2.erase(it); mm2.erase(itlow); it=it1; } } } else break; } }else if(m==2) { ms.clear(); mm.clear(); for (int i=0, j=0; i<n; ++i) { while (j<n && p[j].r[0]<=avl[0]) { ms.insert(p[j].r[1]); mm.insert(mp(p[j].r[1], j)); j++; } multiset<int>::iterator it=ms.upper_bound(avl[1]), it1; // if(it!=ms.end())printf("t!!! %d %d\n", *it, i); if(it!=ms.begin()) { while (it!=ms.begin()) { it1=it; it--; if(*it<=avl[1]) { multimap<int, int>::iterator itlow=mm.lower_bound(*it); int pos=itlow->sc; p[pos].rls=true; for (int i=0; i<m; ++i)avl[i]+=p[pos].a[i]; // printf("pos==%d %d\n", pos, j); // for (int i=0; i<m; ++i)printf("%d ", avl[i]); // puts(""); ms.erase(it); mm.erase(itlow); it=it1; } } } else break; } } for (int i=0; i<n; ++i) if(p[i].rls==false)flag=false; if(flag)puts("Yes"); else puts("No"); } return 0; } /* 5 1 1 0 1 1 1 5 4 3 2 1 1 5 2 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 2 2 0 1 5 2 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 1 2 2 0 1 5 3 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 2 2 0 0 1 5 3 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 2 2 0 0 1 */
2482 Transit search
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> #include <map> using namespace std; typedef long long ll; const int maxn=500+123; const int inf=0x3f3f3f3f; struct Edge{ int v, w, next; }edge[maxn*maxn]; int head[maxn], cnt; struct Node{ int u, w; bool operator < (Node a)const { return w > a.w; } }; void addedge(int u, int v, int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } int dist[maxn]; void Dijkstra(int s, int t){///s是源点 memset (dist, 0x3f, sizeof(dist)); dist[s]=0; priority_queue<Node> q;/// 需要<queue> Node cur; cur.u=s; cur.w=0; q.push(cur); while (!q.empty()){ cur=q.top(); q.pop(); if(dist[cur.u]<cur.w)continue; for (int p=head[cur.u]; ~p; p=edge[p].next) if(dist[edge[p].v]>dist[cur.u]+edge[p].w){ dist[edge[p].v]=dist[cur.u]+edge[p].w; ///if(edge[p].v==t)return; Node tmp; tmp.u=edge[p].v; tmp.w=dist[edge[p].v]; q.push(tmp); } } } void init () { cnt=0; memset (head, -1, sizeof(head)); } int sx[3], sy[3]; int nx[5000+123], ny[5000+123]; int dist2(int x, int y, int x1, int y2) { int res=(x1-x)*(x1-x)+(y2-y)*(y2-y); return res; } int main() { int cas; scanf("%d", &cas); while (cas--) { for (int i=0; i<2; ++i) { int p=5120; char str[10]; int x, y; scanf("%s%d%d", str, &x, &y); sx[i]=4*x; sy[i]=4*y; for (int j=0; j<8; ++j) { int dir=str[j]-'0'; if(dir&2)sx[i]+=p; if(dir&1)sy[i]+=p; p/=2; ///printf("%d %lld %lld\n", i, sx[i], sy[i]); } ///printf(" sss =====%I64d %I64d\n", sx[i], sy[i]); } int m; scanf("%d", &m); ///mm.clear(); vector<int> bus[123]; map<string, int>mm; for (int i=0; i<m; ++i) { string str; cin >> str; mm[str]=i; scanf("%d %d", nx+i, ny+i); } int n; scanf("%d", &n); ///printf("%d!!!", n); for (int i=0; i<n; ++i) { ///bus[i].clear(); int k; scanf("%d", &k); string name; for (int j=0; j<k; ++j) { cin >> name; bus[i].push_back(mm[name]); } } if(dist2(sx[0], sy[0], sx[1], sy[1])<=4000000) { puts("walk there"); continue; } init(); for (int i=0; i<n; ++i) { int sz=bus[i].size(); bool st=false, ed=false; for(int j=0; j<sz; ++j) { int ii=bus[i][j]; if(dist2(sx[1], sy[1], nx[ii], ny[ii])<=1000000) { ed=true; ///addedge(i+1, n+1, 0); ///printf("!!!%d %d\n", i+1, n+1); } if(dist2(sx[0], sy[0], nx[ii], ny[ii])<=1000000) { st=true; ///addedge(0, i+1, 0); ///printf("!!!!!!!!%d %d\n", 0, i+1); } } if(ed)addedge(i+1, n+1, 0); if(st)addedge(0, i+1, 1); for (int j=0; j<n; ++j)if(i!=j) { int sz1=bus[j].size(); bool flag=false; for (int k=0; k<sz; ++k) for (int p=0; p<sz1; ++p) if(bus[i][k]==bus[j][p]) { flag=true; break; } if(flag) { addedge(i+1, j+1, 1); ///printf("!!!!%d %d\n", i+1, j+1); } } } Dijkstra(0, n+1); if(dist[n+1]==inf)puts("take a taxi"); else printf("%d\n", dist[n+1]); } return 0; } /* 10 00000000 1 1 00001000 3 3 4 a 1 1 b 20 30 c 40 50 d 100 100 2 3 a b c 3 b c d 00000000 1 1 03231130 5 5 5 a 1 1 b 2 2 c 3000 3000 d 3000 4000 e 4500 4000 2 2 a b 3 c d e 00000000 1 1 03231130 5 5 5 a 1 1 b 1000 1000 c 3000 3000 d 3000 4000 e 4500 4000 3 3 a b e 3 c d e 3 a b c 00000000 1 1 03231130 5 5 4 a 1 1 b 1000 1000 c 3000 3000 d 3000 4000 2 3 a b c 3 b c d */