URAL的几道题目

本文从WordPress迁移而来, 查看全部WordPress迁移文章

A : URAL 1774 Barber of the Army of Mages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iterator>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define CL(xx,yy) memset((xx),(yy),sizeof((xx)))
#define MAXN 100
#define MAXT 2000
#define S 2101
#define T 2102
#define INF 0x3f3f3f3f

int n,m,ST,ED;
int tot,head[MAXN+MAXT+10];
struct edge{
int u,v,cap,flow,next;
}e[2*(MAXT+MAXT*MAXN+MAXN+1111)];
struct per{
int st,ed;
}a[MAXN+10];
int delta[MAXN+MAXT+10];
int path[MAXN+MAXT+10];
queue<int>que;
vector<PII>ans,__ans__;

inline void add(int u,int v,int cap){
e[tot].u = u;
e[tot].v = v;
e[tot].cap = cap;
e[tot].flow = 0;
e[tot].next = head[u];
head[u] = tot++;
}

bool CMP(PII x , PII y){
if(x.first == y.first)
return x.second < y.second;
return x.first < y.first;
}

void bfs(){
while(!que.empty()) que.pop();
CL(delta,0); delta[S] = INF;
CL(path,-1);
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int k = head[u]; k != -1; k = e[k].next){
int v = e[k].v;
int cap = e[k].cap;
int flow = e[k].flow;
if(delta[v] == 0 && cap > flow){
if(delta[u] < cap - flow) delta[v] = delta[u];
else delta[v] = cap - flow;
path[v] = k;
que.push(v);
}
}
}
}

bool check(PII x){
bool canfind = false;
for(vector<PII>::iterator it = __ans__.begin(); it != __ans__.end(); it++){
if(x.first == (*it).first && x.second == (*it).second){
canfind = true;
break;
}
}
return (!canfind);
}

void EK(){
int FLOW = 0;
__ans__.clear();
ans.clear();
while(true){
bfs();
if(delta[T] == 0) break;
FLOW += delta[T];
for(int k = path[T]; k != -1; k = path[e[k].u]){
int u = e[k].u;
int v = e[k].v;
e[k].flow += delta[T];
e[k^1].flow -= delta[T];
if(u != S && u != T && v != S && v != T){
//printf("%d %d\n",u,v);
if(u < v) ans.PB(MP(v,u));
else __ans__.PB(MP(u,v));
}
}
}
//printf("FLOW = %d\n",FLOW);
if(FLOW < 2*n){ puts("No"); return ; }
puts("Yes");
sort(ans.begin(),ans.end(),CMP);
sort(__ans__.begin(),__ans__.end(),CMP);
int flag = 0;
for(vector<PII>::iterator it = ans.begin(); it != ans.end(); it++){
if( !check(*it) ) continue;
if(flag&1) printf(" %d\n",(*it).second);
else printf("%d",(*it).second);
flag ^= 1;
}
}

int main(){
while(scanf("%d%d",&n,&m)!=EOF){
ST = INF; ED = -1;
for(int i = 1; i <= n; i++){
scanf("%d%d",&a[i].st,&a[i].ed);
a[i].ed = a[i].ed + a[i].st - 1;
ST = min(ST,a[i].st);
ED = max(ED,a[i].ed);
}
tot = 0; CL(head,-1);
for(int t = ST; t <= ED; t++){
add(S,t,m);
add(t,S,0); //反边
for(int i = 1; i <= n; i++){
if(a[i].st <= t && t <= a[i].ed){
add(t,MAXT+i,1);
add(MAXT+i,t,0);
}
}
}
for(int i = 1; i <= n; i++){
add(MAXT+i,T,2);
add(T,MAXT+i,0);
}
EK();
}
return 0;
}

D : URAL 1777 Anindilyakwa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iterator>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define CL(xx,yy) memset((xx),(yy),sizeof((xx)))
#define INF 1000000000000001110
LL x,y,z;
vector<LL>num;

int main(){
while(scanf("%I64d%I64d%I64d",&x,&y,&z)!=EOF){
num.clear();
num.PB(x);
num.PB(y);
num.PB(z);
sort(num.begin(),num.end());
LL ans = 0LL;
while(true){
ans++;
LL Min = INF;
int size = num.size();
for(int i = 0 ; i < size-1; i++)
if(num[i+1] - num[i] < Min){ Min = num[i+1] - num[i]; }
if(Min == 0LL) break;
num.PB(Min);
sort(num.begin(),num.end());
}
printf("%I64d\n",ans);
}
return 0;
}

F : URAL 1779 The Great Team

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iterator>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define CL(xx,yy) memset((xx),(yy),sizeof((xx)))
#define N 10010

vector<PII>edge;
int L[N],R[N],n,idf,idr;
int de[N];

int main(){
int nn;
while(scanf("%d",&nn)!=EOF){
n = nn;
if(n & 1) n--;
edge.clear();
CL(de,0);
for(int i = 1; i <= (n>>1); i++){
L[i] =i*2-1 ; R[i] = i*2;
}
/*
for(int i = 1; i <= (n>>1); i++)
printf("%d ",L[i]);
puts("");
for(int i = 1; i <= (n>>1); i++)
printf("%d ",R[i]);
puts("");
*/
idf = 1; idr = n>>1;
while(idf < idr){
int u,v;
for(int i = idf; i < idr; i++){
u = L[i]; v = L[idr];
de[u]++; de[v]++;
edge.PB(MP(u,v));
u = R[i]; v = R[idr];
de[u]++; de[v]++;
edge.PB(MP(u,v));
}
u = L[idr]; v = R[idr];
if(de[u] == idr) continue;
de[u]++; de[v]++;
edge.PB(MP(u,v));
idf++; idr--;
}
if(idf == idr){
int u = L[idf];
int v = R[idf];
if(de[u] < idf){
de[u]++; de[v]++;
edge.PB(MP(u,v));
}
}
/*
for(int i = 1; i <= nn; i++)
printf("%d[%d]\n",i,de[i]);
*/
printf("%d\n",edge.size());
for(int i = 0 ; i < edge.size(); i++)
printf("%d %d\n",edge[i].first,edge[i].second);
}
return 0;
}

J : URAL 1783 Nuclear Arms Race

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iterator>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define CL(xx,yy) memset((xx),(yy),sizeof((xx)))
#define N 11111
#define INF 0x3f3f3f3f

int n,m,a[N];

int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 1; i < n+m; i++)
scanf("%d",&a[i]);
int sum = 0;
for(int i = 1; i <= n; i++){
int Max = -INF;
for(int j = i; j <= i+m-1; j++){
int t = (int)ceil((a[j]*1.0-sum) / (j-i+1));
Max = max(Max,t);
}
printf("%d ",Max);
sum += Max;
}
puts("");
}
return 0;
}