2015 Multi-University Training Contest 1 题解 BY ME
多校第一场结束了,先来没事整理了一下代码
http://blog.sina.com.cn/s/blog_15139f1a10102vnx5.html (这是题解,没有代码的题解)
下面是我根据题解写出的代码
第一题 hdu 5288 OO’s Sequence
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <sstream> #include <cstdlib> #include <complex> #include <cstring> #include <iostream> #include <algorithm> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = (N);i >= 0;i--) #define DWN_1(i,N) for (int i = (N);i >= 1;i--) #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) #define FR(N) freopen((N),"r",stdin) #define FW(N) freopen((N),"w",stdout) #define MAXN 2010 #define GETS(ch) fgets((ch),MAXN,stdin) #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; typedef long long LL; typedef LL ll; typedef map<int,LL> MINT; typedef map<char,int> MCH; typedef map<string,int> MSTR; typedef vector<int> VINT; typedef set<LL> SINT; typedef pair<int,int> PINT; LL read(){ LL ret=0,f=1; char x=getchar(); while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();} while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar(); return ret*f; } VINT v[10010]; //factor VINT vec[10010]; // get the position of every number; //get everynumber's factor void init() { for (int i = 1;i < 10010;i++) { for (int j = 1;j * j <= i;j++) { if (i % j == 0) { v[i].push_back(j); if (j * j != i) v[i].push_back(i / j); } } } } int main () { // FR; int N; init(); int a[100010]; while (~scanf("%d",&N)) { REP(i,10010) { vec[i].clear(); } REP(i,N) { scanf("%d",&a[i]); vec[a[i]].push_back(i); // get the position } int ans = 0; REP(i,N) { int mini = N; int maxn = -1; REP(j,v[a[i]].size()) { int factor = v[a[i]][j]; REP(k,vec[factor].size()) { int pos = vec[factor][k]; if (pos < i) { maxn = max(maxn,pos); } else if (pos > i) { mini = min(mini,pos); } } } ans += (mini - i) * (i - maxn); ans = ans % (int)(1e9 + 7); } cout << ans << endl; } }
第二题 hdu5289 Assignment
// ST算法 http://blog.csdn.net/insistgogo/article/details/9929103 (ST算法讲解)
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <sstream> #include <cstdlib> #include <complex> #include <cstring> #include <iostream> #include <algorithm> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = (N);i >= 0;i--) #define DWN_1(i,N) for (int i = (N);i >= 1;i--) #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) #define FR(N) freopen((N),"r",stdin) #define FW(N) freopen((N),"w",stdout) #define MAXN 100000 + 5 #define GETS(ch) fgets((ch),MAXN,stdin) #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; typedef long long LL; typedef LL ll; typedef map<int,LL> MINT; typedef map<char,int> MCH; typedef map<string,int> MSTR; typedef vector<int> VINT; typedef set<LL> SINT; typedef pair<int,int> PINT; LL read(){ LL ret=0,f=1; char x=getchar(); while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();} while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar(); return ret*f; } int A[MAXN]; int MIN[MAXN][30], MAX[MAXN][30]; void RMQ_init(int N) { REP(i,N) MAX[i][0] = MIN[i][0] = A[i]; for(int j = 1; (1 << j) <= N; j++) { for(int i = 0; i + (1 << j) - 1 < N; i++) { MAX[i][j] = max(MAX[i][j - 1], MAX[i + (1 << (j - 1))][j - 1]); MIN[i][j] = min(MIN[i][j - 1], MIN[i + (1 << (j - 1))][j - 1]); } } } int RMQ_min(int L, int R) { int k = 0; while((1 << (k + 1)) <= R - L + 1) k++; return min(MIN[L][k], MIN[R - (1 << k) + 1][k]); } int RMQ_max(int L, int R) { int k = 0; while((1 << (k + 1)) <= R - L + 1) k++; return max(MAX[L][k], MAX[R - (1 << k) + 1][k]); } int main() { int T; // freopen("1.in","r",stdin); scanf("%d", &T); while(T--) { int N, k; scanf("%d%d", &N, &k); REP(i,N) scanf("%d", &A[i]); RMQ_init(N); LL ans = 0; int pos = 0; REP(i,N) { int low = i, right = N - 1; while(low <= right) { int mid = (low + right) >> 1; if(RMQ_max(i, mid) - RMQ_min(i, mid) < k){ pos = mid; low = mid + 1; } else { right = mid - 1; } } ans += pos - i + 1; } cout << ans << endl; } return 0; }
// 单调队列 #include <map> #include <set> #include <list> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <sstream> #include <cstdlib> #include <complex> #include <cstring> #include <iostream> #include <algorithm> #include <deque> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = (N);i >= 0;i--) #define DWN_1(i,N) for (int i = (N);i >= 1;i--) #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) #define FR(N) freopen((N),"r",stdin) #define FW(N) freopen((N),"w",stdout) #define MAXN 2010 #define GETS(ch) fgets((ch),MAXN,stdin) #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; typedef long long LL; typedef LL ll; typedef map<int,LL> MINT; typedef map<char,int> MCH; typedef map<string,int> MSTR; typedef vector<int> VINT; typedef set<LL> SINT; typedef pair<int,int> PINT; LL read(){ LL ret=0,f=1; char x=getchar(); while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();} while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar(); return ret*f; } // DQ1 increase DQ2 decrease deque<int> DQ1,DQ2; int a[100010]; int main () { // FR("1.in"); int T; cin >> T; while (T--) { int N,K; DQ1.clear(); DQ2.clear(); scanf("%d %d",&N,&K); REP(i,N) scanf("%d",&a[i]); ll ans = 0; for (int i = 0,j = 0;i < N;i++) { while (!DQ1.empty() && a[DQ1.back()] > a[i]) DQ1.pop_back(); while (!DQ2.empty() && a[DQ2.back()] < a[i]) DQ2.pop_back(); DQ1.push_back(i); DQ2.push_back(i); while (a[DQ2.front()] - a[DQ1.front()] >= K) { if (j == DQ2.front()) DQ2.pop_front(); if (j == DQ1.front()) DQ1.pop_front(); j++; } ans += i - j + 1; } cout << ans << endl; } }
// 树状数组 #include <map> #include <set> #include <list> #include <stack> #include <queue> #include <deque> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <sstream> #include <cstdlib> #include <complex> #include <climits> #include <cstring> #include <iostream> #include <algorithm> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = (N);i >= 0;i--) #define DWN_1(i,N) for (int i = (N);i >= 1;i--) #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) #define FR(N) freopen((N),"r",stdin) #define FW(N) freopen((N),"w",stdout) #define MAXN 2010 #define GETS(ch) fgets((ch),MAXN,stdin) #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; typedef long long LL; typedef LL ll; typedef map<int,LL> MINT; typedef map<char,int> MCH; typedef map<string,int> MSTR; typedef vector<int> VINT; typedef set<LL> SINT; typedef pair<int,int> PINT; LL read(){ LL ret=0,f=1; char x=getchar(); while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();} while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar(); return ret*f; } int a[100010]; int N,K; int maxn[100010]; int mini[100010]; int lowbit(int i) { return i & (-i); } void add(int i,int value) { while (i <= N) { maxn[i] = max(maxn[i],value); mini[i] = min(mini[i],value); i += lowbit(i); } } void init() { REP_1(i,N + 1) maxn[i] = INT_MIN; REP_1(i,N + 1) mini[i] = INT_MAX; } int query_min(int i) { int MIN = INT_MAX; while (i > 0) { MIN = min(MIN,mini[i]); i -= lowbit(i); } return MIN; } int query_max(int i) { int MAX = INT_MIN; while (i > 0) { MAX = max(MAX,maxn[i]); i -= lowbit(i); } return MAX; } int main () { // FR("1.in"); int T; cin >> T; while (T--) { scanf("%d %d",&N,&K); init(); REP_1(i,N + 1) scanf("%d",&a[i]); ll ans = 0; DWN_1(i,N) { add(i,a[i]); int low = i; int hight = N; int pos = 0; while (low <= hight) { int mid = (hight + low) >> 1; if (query_max(mid) - query_min(mid) < K) { pos = mid; low = mid + 1; } else { hight = mid - 1; } } ans += pos - i + 1; } cout << ans << endl; } }
第七题 hdu5293 Tricks Device
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <deque> #include <cmath> #include <ctime> #include <cstdio> #include <vector> #include <sstream> #include <cstdlib> #include <complex> #include <climits> #include <cstring> #include <iostream> #include <algorithm> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = (N);i >= 0;i--) #define DWN_1(i,N) for (int i = (N);i >= 1;i--) #define DWN_2(i,en,be) for (int i = (en);i >= (be);i--) #define FR(N) freopen((N),"r",stdin) #define FW(N) freopen((N),"w",stdout) #define MAXN 2010 #define GETS(ch) fgets((ch),MAXN,stdin) #define INF 0x3f3f3f3f #define MOD 1000000007 using namespace std; typedef long long LL; typedef LL ll; typedef map<int,LL> MINT; typedef map<char,int> MCH; typedef map<string,int> MSTR; typedef vector<int> VINT; typedef set<LL> SINT; typedef pair<int,int> PINT; LL read(){ LL ret=0,f=1; char x=getchar(); while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();} while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar(); return ret*f; } class EdmondsKarp { private: struct Edge{ int from,to,flow; Edge(int u,int v,int f):from(u),to(v),flow(f){} }; public: int n; vector<Edge> edges; VINT G[MAXN]; int a[MAXN]; int p[MAXN]; int s,t; int used[MAXN]; void init(int n,int s,int t) { this -> n = n; this -> s = s; this -> t = t; REP(i,n) G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int flow) { edges.push_back(Edge(from,to,flow)); edges.push_back(Edge(to,from,0)); G[from].push_back(edges.size() - 2); G[to].push_back(edges.size() - 1); } int BFS_Maxflow(int s,int t) { int flow = 0; while (1) { memset(a,0,sizeof(a)); queue<int> Q; Q.push(s); a[s] = INF; while(!Q.empty()) { int x = Q.front();Q.pop(); REP(i,G[x].size()) { Edge& e = edges[G[x][i]]; if (!a[e.to] && e.flow > 0) { p[e.to] = G[x][i]; a[e.to] = min(a[x],e.flow); Q.push(e.to); } } if (a[t]) break; } if (!a[t]) break; for (int i = t;i != s;i = edges[p[i]].from) { edges[p[i]].flow -= a[t]; edges[p[i]^1].flow += a[t]; } flow += a[t]; } return flow; } int dfs(int s,int t,int f) { if (s == t) return f; used[s] = 1; REP(i,G[s].size()) { Edge& e = edges[G[s][i]]; if (!used[e.to] && e.flow > 0) { int d = dfs(e.to,t,min(f,e.flow)); if (d > 0) { e.flow -= d; edges[G[s][i]^1].flow += d; return d; } } } return 0; } int DFS_Maxflow(int s,int t) { int flow = 0; while (1) { memset(used,0,sizeof(used)); int f = dfs(s,t,INF); if (f == 0) return flow; flow += f; } return flow; } }E; class Dijkstra { private: struct Edge{ int from,to,dist; Edge(int u,int v,int d):from(u),to(v),dist(d) {} }; int n; vector<Edge> edges; VINT G[MAXN]; bool done[MAXN]; int d[MAXN]; VINT path[MAXN]; int number[MAXN]; public: void init(int n) { this -> n = n; edges.clear(); REP(i,n) { G[i].clear(); number[i] = INF; path[i].clear(); } } void AddEdge(int from,int to,int dist){ edges.push_back(Edge(from,to,dist)); G[from].push_back(edges.size() - 1); } void dijkstra(int s) { priority_queue<PINT,vector<PINT>,greater<PINT> > Q; REP(i,n) d[i] = INF; d[s] = 0; memset(done,false,sizeof(done)); Q.push(make_pair(d[s],s)); while (!Q.empty()) { PINT P = Q.top();Q.pop(); int u = P.second; if (done[u]) continue; done[u] = true; REP(i,G[u].size()) { Edge& e = edges[G[u][i]]; if (d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; path[e.to].clear(); path[e.to].push_back(u); Q.push(make_pair(d[e.to],e.to)); } else if (d[e.to] == d[u] + e.dist){ path[e.to].push_back(u); Q.push(make_pair(d[e.to],e.to)); } } } } int dfs(int end,int s) { if (end == s) return 0; if (number[end] != INF) return number[end]; REP(i,path[end].size()) { int fa = path[end][i]; E.AddEdge(fa,end,1); number[end] = min(number[end],dfs(fa,s) + 1); } return number[end]; } }D; int main () { // FR("1.in"); int N,M; while (~scanf("%d %d",&N,&M)) { D.init(N); REP(i,M) { int u,v,c; scanf("%d %d %d",&u,&v,&c); u --; v --; D.AddEdge(u,v,c); D.AddEdge(v,u,c); } D.dijkstra(0); int dis = D.dfs(N - 1,0); cout << E.DFS_Maxflow(0,N - 1) << " " << M - dis << endl; } }