标签:set memset turn empty dff class 计算 freopen out
4 3 6 2 5
2 2 3
思路
Task1:数据范围很小,暴力出LIS长度就可以了
Task2:显然是个带限制条件的最小路径覆盖问题
拆点建图,以该点是否为LIS的起终点为依据判断是否对ST连边
对所有点肯定要满足该点出点要连下一个点的入点
Task3:取消对1点和n点的限制,计算出对残量网络的影响
如果由残量网络,就继续加流直至跑满。
Note:对 n 为 1 的时候显然需要特判一下。
CODE
1 #include <bits/stdc++.h> 2 #define dbg(x) cout << #x << "=" << x << endl 3 #define eps 1e-8 4 #define pi acos(-1.0) 5 6 using namespace std; 7 typedef long long LL; 8 9 const int inf = 0x3f3f3f3f; 10 11 template<class T>inline void read(T &res) 12 { 13 char c;T flag=1; 14 while((c=getchar())<‘0‘||c>‘9‘)if(c==‘-‘)flag=-1;res=c-‘0‘; 15 while((c=getchar())>=‘0‘&&c<=‘9‘)res=res*10+c-‘0‘;res*=flag; 16 } 17 18 namespace _buff { 19 const size_t BUFF = 1 << 19; 20 char ibuf[BUFF], *ib = ibuf, *ie = ibuf; 21 char getc() { 22 if (ib == ie) { 23 ib = ibuf; 24 ie = ibuf + fread(ibuf, 1, BUFF, stdin); 25 } 26 return ib == ie ? -1 : *ib++; 27 } 28 } 29 30 int qread() { 31 using namespace _buff; 32 int ret = 0; 33 bool pos = true; 34 char c = getc(); 35 for (; (c < ‘0‘ || c > ‘9‘) && c != ‘-‘; c = getc()) { 36 assert(~c); 37 } 38 if (c == ‘-‘) { 39 pos = false; 40 c = getc(); 41 } 42 for (; c >= ‘0‘ && c <= ‘9‘; c = getc()) { 43 ret = (ret << 3) + (ret << 1) + (c ^ 48); 44 } 45 return pos ? ret : -ret; 46 } 47 48 const int maxn = 200007; 49 50 int n, m; 51 int s, t; 52 53 struct edge{ 54 int from,to; 55 LL cap,flow; 56 }; 57 58 map<int, int> vis; 59 60 struct DINIC { 61 int head[maxn << 1], nxt[maxn << 1], edge[maxn << 1], cnt; 62 int cap[maxn << 1], depth[maxn << 1]; 63 64 void init() { 65 cnt = 1; 66 memset(head, 0, sizeof(head)); 67 memset(cap, 0, sizeof(cap)); 68 memset(depth, 0, sizeof(depth)); 69 vis.clear(); 70 } 71 72 void BuildGraph(int u, int v, int w) { 73 ++cnt; 74 edge[cnt] = v; 75 nxt[cnt] = head[u]; 76 cap[cnt] = w; 77 head[u] = cnt; 78 79 ++cnt; 80 edge[cnt] = u; 81 nxt[cnt] = head[v]; 82 cap[cnt] = 0; 83 head[v] = cnt; 84 } 85 86 queue<int> q; 87 88 bool bfs() { 89 memset(depth, 0, sizeof(depth)); 90 depth[s] = 1; 91 q.push(s); 92 while(!q.empty()) { 93 int u = q.front(); 94 q.pop(); 95 for ( int i = head[u]; i; i = nxt[i] ) { 96 int v = edge[i]; 97 if(depth[v]) { 98 continue; 99 } 100 if(cap[i]) { 101 depth[v] = depth[u] + 1; 102 q.push(v); 103 } 104 } 105 } 106 return depth[t]; 107 } 108 109 int dfs(int u, int dist) { 110 if(u == t) { 111 return dist; 112 } 113 int flow = 0; 114 for ( int i = head[u]; i && dist; i = nxt[i] ) { 115 if(cap[i] == 0) 116 continue; 117 int v = edge[i]; 118 if(depth[v] != depth[u] + 1) { 119 continue; 120 } 121 int res = dfs(v, min(cap[i], dist)); 122 cap[i] -= res; 123 cap[i ^ 1] += res; 124 //printf("cap[%d]:%d\n",t, cap[t]); 125 dist -= res; 126 flow += res; 127 } 128 return flow; 129 } 130 131 int maxflow() { 132 int ans = 0; 133 while(bfs()) { 134 ans += dfs(s, inf); 135 } 136 return ans; 137 } 138 } dinic; 139 140 int a[maxn]; 141 int f[maxn]; 142 143 int main() 144 { 145 //freopen("data.txt", "r", stdin); 146 read(n); 147 if(n == 1) { 148 printf("1\n1\n1\n"); 149 return 0; 150 } 151 for ( int i = 1; i <= n; ++i ) { 152 read(a[i]); 153 f[i] = 1; 154 } 155 int len = 1; 156 for ( int i = 2; i <= n; ++i ) { 157 for ( int j = 1; j < i; ++j ) { 158 if(a[j] <= a[i]) { 159 f[i] = max(f[i], f[j] + 1); 160 } 161 } 162 len = max(len, f[i]); 163 } 164 // for ( int i = 1; i <= n; ++i ) { 165 // printf("f[%d]:%d\n",i, f[i]); 166 // } 167 cout << len << endl; 168 dinic.init(); 169 s = 0, t = maxn; 170 for ( int i = 1; i <= n; ++i ) { 171 if(f[i] == 1) { 172 dinic.BuildGraph(s, i, 1); 173 } 174 if(f[i] == len) { 175 dinic.BuildGraph(i + n, t, 1); 176 } 177 dinic.BuildGraph(i, i + n, 1); 178 } 179 for ( int i = 1; i <= n; ++i ) { 180 for ( int j = i + 1; j <= n; ++j ) { 181 if(a[j] >= a[i]) { 182 if(f[i] == f[j] - 1) { 183 dinic.BuildGraph(i + n, j, 1); 184 } 185 } 186 } 187 } 188 int ans = dinic.maxflow(); 189 cout << ans << endl; 190 dinic.BuildGraph(s, 1, inf);dinic.BuildGraph(1, 1 + n, inf); 191 if(f[n] == len) { 192 dinic.BuildGraph(n * 2, t, inf); dinic.BuildGraph(n, n * 2, inf); 193 } 194 ans += dinic.maxflow(); 195 cout << ans << endl; 196 return 0; 197 }
标签:set memset turn empty dff class 计算 freopen out
原文地址:https://www.cnblogs.com/orangeko/p/12772313.html