ACM-ICPC 2017 Asia HongKong 解题报告
任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKong
按AC次序:
D - Card collection
In an online game, a player can collect different types of power cards. Each power card can enable a player to have a unique game magic. There are m power cards available in the game as (P_1, . . . , P_m)(P1,...,Pm). AA power card can be acquired by game points or through trading with others. In order to support the trading easier, a platform has been built. The platform charges a fixed amount C_iCi,jj game points for trading respective power cards,P_iPi and P_jPj. Note. Trading P_iPi to P_jPj or P_jPj to P_iPi would be of the same charge.
Write a program to calculate the minimal number of game points with a given original power card (P o)(Po) to a target one (Pt)(Pt). The output of your program should be the minimal game point value.
Input
The test data may contain many test cases. Each test case contains three data sections. The first section is an integer to indicate the number of power card types m (1 < m \le 50)m(1<m≤50). The second section contains two integers representing the original power card Po(0 < Po \le m)(0<Po≤m) and the target power card Pt (0 < Pt \le m)Pt(0<Pt≤m). Also, Po cannot be the same as Pt. The third section has a set of triplets and each triplet contains two cards id ii, jj and the charge amount c_i,j (0 < c_i,j \le 20)ci,j(0<ci,j≤20) between 22 types of power cards (P_i,P_j)(Pi,Pj). The end part of section 33contains a single 00.
Output
The output for each test case is the minimal number of game points needed for the trading.
样例输入复制
5
2 4
1 2 1
2 3 4
5 4 2
3 4 1
2 5 2
0
7
6 7
1 2 4
1 3 2
1 6 1
2 7 1
3 4 2
4 7 1
4 5 1
5 6 2
0
样例输出复制
4
4
题意概括:
一道英文题,其实讲的是:有N个结点,输入起点 o 终点 t ,输入路径建无向图,求 o 到 t 的最短路。
解题思路:
静态邻接表建图,stl优先队列优化的Dijsktra求单源最短路。
AC code:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <deque>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e3 + ;
typedef pair<int, int> HeapNode;
struct edge
{
int v, nxt, w;
}G[MAXN*];
int head[MAXN], dis[MAXN];
int N, M, cnt; inline void init()
{
for(int i = ; i <= N; i++)
head[i] = -, dis[i] = INF;
cnt = ;
} inline void add(int from, int to, int we)
{
G[cnt].w = we;
G[cnt].v = to;
G[cnt].nxt = head[from];
head[from] = cnt++;
} void dij(int st)
{
//memset(dis, INF, sizeof(dis));
priority_queue<HeapNode, vector<HeapNode>, greater<HeapNode> > heap; //申请优先队列,以键值1排序
dis[st] = ;
heap.push(make_pair(, st));
while(!heap.empty())
{
pair<int, int>T = heap.top();
heap.pop(); if(T.first != dis[T.second]) continue; for(int i = head[T.second]; i != -; i = G[i].nxt)
{
int v = G[i].v;
if(dis[v] > dis[T.second] + G[i].w)
{
dis[v] = dis[T.second] + G[i].w;
heap.push(make_pair(dis[v], v));
}
}
}
} int main()
{
int a, b, c;
while(~scanf("%d", &N))
{
int st, ed;
scanf("%d%d", &st, &ed);
//if(N == 0 && M == 0) break;
init();
while(~scanf("%d", &a))
{
if(a == ) break;
scanf("%d%d", &b, &c);
add(a, b, c);
add(b, a, c);
} dij(st);
printf("%d\n", dis[ed]);
} return ;
}
E - Base Station Sites
5 is the proposed next telecommunications standards beyond the current 4G4G standards. 5G5G planning aims at higher capacity than current 4G4G, allowing a higher density of mobile broadband users, and supporting device-to- device, reliable, and massive wireless communications. AA telecommunication company would like to install more base stations to provide better communication for customers. Due to the installation cost and available locations, the company can only install S (2 \le S \le L)S(2≤S≤L) base stations at L (2 \le L \le 100, 000)L(2≤L≤100,000) candidate locations. Since the base stations work in the same frequency band, they will interfere and cause severe performance degradation. To provide high quality communication experience to customers, the company would like to maximize the distance between the base stations so as to reduce the wireless interference among the base stations. Suppose the L candidate locations are in a straight line at locations P_1, P_2, ... , PL (0 \le Pi \le 1, 000, 000)P1,P2,...,PL(0≤Pi≤1,000,000) and the company wants to install SS base stations at the LL candidate locations. What is the largest minimum distance among the SS base stations?
Input
The input data includes multiple test sets.
Each set starts with a line which specifies LL (i.e.i.e., the number of candidate locations) and SS (i.e.i.e., the number of base stations). The next line contains LL space-separated integers which represent P_1P1 , P_2P2 , ... , P_LPL . The input data ends “00 00”.
Output
For each set, you need to output a single line which should be the largest minimum distance among the base stations.
For the first set, the 33 base stations can be installed at locations 2, 6, 112,6,11.
样例输入复制
5 3
2 3 9 6 11
4 3
1 4 9 10
0 0
样例输出复制
4
3
题意概括:
有 N 个坐标(非有序),在这N个坐标里选S个,使得两两间最小的距离最大化。
即:POJ 2456
解题思路:
二分搜索 最大化最小值
C( d ):可以安排基站的位置使得最近两个基站距离不小于 d;
那么问题就变成了求满足 C( d )的最大 d 。另外,最近的间距不小于 d 也就说明所有基站距离都不小于 d。
C( d ): 可以安排基站的位置使得任意基站的间距都不小于 d。
贪心法求解这个问题:
①对基站坐标进行排序
②把第一个基站放进 P0 坐标
③如果第 i 个基站放在 Pj 的话,第 i+1 个基站就要放入满足 Pj+d <= Pk 的最小的 Pk 中。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e5+; int P[MAXN];
int N, M; bool c(int d)
{
int last = ;
int crt = ;
for(int i = ; i < M; i++){
crt = last+;
while(crt < N && P[crt] - P[last] < d) crt++;
if(crt == N) return false;
last = crt;
}
return true;
} int main()
{
while(~scanf("%d %d", &N, &M) && (N+M)){
for(int i = ; i < N; i++)
scanf("%d", &P[i]);
sort(P, P+N);
int st = , ed = INF;
int mid = ;
while(ed - st > ){
mid = (st+ed)/;
if(c(mid)) st = mid;
else ed = mid;
}
printf("%d\n", st);
}
return ;
}
F - Nearby Bicycles
With fast developments of information and communication technology, many cities today have established bicycle sharing systems. The key component of the system is to provide information on nearby bicycles to potential users.
Consider mm bicycles and nn customers, where each bicycle is located at coordinate (c_j , d_j )(cj,dj) for j = 1, 2, ... , m,j=1,2,...,m,and each user ii is located at coordinate (a_i, b_i)(ai,bi) for i = 1, 2, ... , ni=1,2,...,n The distance between two coordinates (x, y)(x,y)and (x, y)(x,y) is measured by \sqrt{(x-x)^2 +(y-y)^2}(x−x)2+(y−y)2. For each user i = 1,2,...,ni=1,2,...,n, you are given a threshold s_isi, your task is to return the total number of bicycles that are within a distance of si from user ii.
Input
The test data may contain many test cases. Each test case contains four lines. The first line of each case contains two integers, mm and n (0 < m, n \le 1000)n(0<m,n≤1000). The second line contains the coordinates, (c_1, d_1), (c_2, d_2), ... , (c_m, d_m)(c1,d1),(c2,d2),...,(cm,dm), of bicycles 1, 2, ... , m1,2,...,m, respectively, which are separated by a space. The third line contains the coordinates,(a1, b1), (a2, b2), ... , (an, bn)(a1,b1),(a2,b2),...,(an,bn), of users 1, 2,... , n1,2,...,n, respectively, which are separated by a space. contains the thresholds, s_1, s_2, ... , s_ns1,s2,...,sn, of the nn users. The last test case is followed by a line of two 00s. All the number of coordinate in the input is in the range [-100000, 100000][−100000,100000].
Output
The output for each test case contains a line of nn integers, k_1, k_2, ... , k_nk1,k2,...,kn, where each ki represents the total number of bicycles that are within a distance of s_isi from user ii, for i = 1,2,...,ni=1,2,...,n.
样例输入复制
4 2
(0,0) (0,1) (1,0) (1,1)
(0,0) (1,1)
1 1
0 0
样例输出复制
3 3
题意概括:
先给出 N 个自行车的坐标, 然后给 M 个人的坐标,依次输出与第 j 个人距离为 sj 的自行车的数量( 1 < j <= M );
解题思路:
暴力纯模拟,不过输入用 sscanf 会方便很多。
输出是要注意格式,最后一个空格不输出。
AC code:
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MOD=1e9+;
const int MAXN=1e3+; using namespace std; struct node{
ll F,S;
}a[MAXN],b[MAXN],t;
ll pos( node a,node b)
{
return ( (a.F-b.F)*(a.F-b.F)+ (a.S-b.S)*(a.S-b.S) );
}
ll R[MAXN];
int V[MAXN];
char s[];
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
getchar();
if(n==&&m==)
break;
memset(V,,sizeof(V));
for(int i=;i<=m;i++)
{
ll x,y;
scanf("%s",s);
sscanf(s,"(%lld,%lld)",&x,&y);
a[i].F=x;
a[i].S=y;
}
getchar();
for(int i= ; i<=n ; i++)
{
ll x,y;
scanf("%s",s);
sscanf(s,"(%lld,%lld)",&x,&y);
b[i].F=x;
b[i].S=y;
}
for(int i= ; i<=n ; i++)
scanf("%lld",&R[i]); for(int i= ; i<=n ; i++)
{
int ans=;
for(int j= ; j<=m ; j++)
{
ll POS=pos(b[i],a[j]);
if(POS<=(R[i]*R[i]))
ans++;
}
V[i]=ans;
} for(int i=;i<=n;i++)
printf("%d%c",V[i],i==n?'\n':' ');
}
return ;
}
B - Black and White
Considerasquaremapwith N \times NN×N cells. We indicate the coordinate of a cell by(i,j)(i,j),where 1 \le i,j \le N1≤i,j≤N. Each cell has a color either white or black. The color of each cell is initialized to white. The map supports the operation flip([x_{low}, x_{high}], [y_{low}, y_{high]})([xlow,xhigh],[ylow,yhigh]), which flips the color of each cell in the rectangle [x_{low}, x_{high}] \times [y_{low}, y_{high}][xlow,xhigh]×[ylow,yhigh]. Given a sequence of flip operations, our problem is to count the number of black cells in the final map. We illustrate this in the following example. Figure (a)(a) shows the initial map. Next, we call flip([2,4],[1,3])([2,4],[1,3]) and obtain Figure (b)(b). Then, we call f lip([1, 5], [3, 5])([1,5],[3,5]) and obtain Figure (c)(c). This map contains 1818 black cells.
Input
The first line contains the number of test cases T (T < 10)T(T<10). Each test case begins with a line containing two integers NN and K (1 < N,K < 10000)K(1<N,K<10000), where NN is the parameter of the map size and KK is the number of flip operations. Each subsequent line corresponds to a flip operation, with four integers: x_{low}x_{high}xlowxhigh, y_{low}, y_{high}ylow,yhigh.
Output
For each test case, output the answer in aa line.
样例输入复制
1
5 2
2 4 1 3
1 5 3 5
样例输出复制
18
题意概括:
黑白两色,矩阵初始化为白色,后面操作把 操作矩阵颜色反转。求黑色方块数量。
解题思路:
树状数组扫描线模板题。
AC code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int n;
int sum[];
int mark[];
void nodeupdate(int root,int l,int r,ll num)
{
mark[root]^=;
sum[root]=(r-l+)-sum[root];
}
void pushdown(int root,int l,int r)//传递给两孩子
{
if(mark[root]==)return;
int mid=(l+r)/;
nodeupdate(root*,l,mid,mark[root]);
nodeupdate(root*+,mid+,r,mark[root]);
mark[root]=;
}
void update(int kl,int kr,ll num, int root=,int l=,int r=n)//区间[kl,kr]修改
{
if(kl<=l&&r<=kr){
nodeupdate(root,l,r,num);
return;
}
pushdown(root,l,r);
int mid=(l+r)/;
if(kl<=mid)
update(kl,kr,num,root*,l,mid);
if(kr>mid)
update(kl,kr,num,root*+,mid+,r);
sum[root]=sum[root*]+sum[root*+];
} struct node{
int h,a,b,flag;
}e[];
int cnt=;
bool cmp(node a,node b){
if(a.h==b.h)return a.flag>b.flag;
return a.h<b.h;
}
int main()
{
int T,m,x1,x2,y1,y2;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(sum,,sizeof(sum));
memset(mark,,sizeof(mark));
cnt=;
for(int i=;i<m;i++)
{
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
e[cnt++]=node{y1,x1,x2,};
e[cnt++]=node{y2,x1,x2,-};
}
sort(e,e+cnt,cmp);
int ans=;
for(int i=,j=;i<=n;i++)
{
while(j<cnt&&e[j].h<=i&&e[j].flag==){
update(e[j].a,e[j].b,);
j++;
}
ans+=sum[];
while(j<cnt&&e[j].h<=i){
update(e[j].a,e[j].b,);
j++;
}
}
cout<<ans<<endl;
}
}
最后自闭 A 题 和 I 题 要用到 JAVA 大数。小蒟蒻不才,来日方长。