ACM-ICPC 2017 Asia HongKong 解題報告


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<m50). The second section contains two integers representing the original power card Po(0 < Po \le m)(0<Pom) and the target power card Pt (0 < Pt \le m)Pt(0<Ptm). 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,j20) 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:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <deque>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <queue>
 8 #include <cmath>
 9 #define INF 0x3f3f3f3f
10 using namespace std;
11 
12 
13 const int MAXN = 1e3 + 50;
14 typedef pair<int, int> HeapNode; 
15 struct edge
16 {
17     int v, nxt, w;
18 }G[MAXN*100];
19 int head[MAXN], dis[MAXN];
20 int N, M, cnt;
21 
22 inline void init()
23 {
24     for(int i = 0; i <= N; i++)
25         head[i] = -1, dis[i] = INF;
26     cnt = 0;
27 }
28 
29 inline void add(int from, int to, int we)
30 {
31     G[cnt].w = we;
32     G[cnt].v = to;
33     G[cnt].nxt = head[from];
34     head[from] = cnt++;
35 }
36 
37 void dij(int st)
38 {
39     //memset(dis, INF, sizeof(dis));
40     priority_queue<HeapNode, vector<HeapNode>, greater<HeapNode> > heap;    //申請優先隊列,以鍵值1排序
41     dis[st] = 0;
42     heap.push(make_pair(0, st));
43     while(!heap.empty())
44     {
45         pair<int, int>T = heap.top();
46         heap.pop();
47 
48         if(T.first != dis[T.second]) continue;
49 
50         for(int i = head[T.second]; i != -1; i = G[i].nxt)
51         {
52             int v = G[i].v;
53             if(dis[v] > dis[T.second] + G[i].w)
54             {
55                 dis[v] = dis[T.second] + G[i].w;
56                 heap.push(make_pair(dis[v], v));
57             }
58         }
59     }
60 }
61 
62 int main()
63 {
64     int a, b, c;
65     while(~scanf("%d", &N))
66     {
67         int st, ed;
68         scanf("%d%d", &st, &ed);
69         //if(N == 0 && M == 0) break;
70         init();
71         while(~scanf("%d", &a))
72         {
73             if(a == 0) break;
74             scanf("%d%d", &b, &c);
75             add(a, b, c);
76             add(b, a, c);
77         }
78 
79         dij(st);
80         printf("%d\n", dis[ed]);
81     }
82 
83     return 0;
84 }
View Code

 

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(2SL) base stations at L (2 \le L \le 100, 000)L(2L100,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(0Pi1,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 “000”.

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:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #define INF 0x3f3f3f3f
 7 using namespace std;
 8 const int MAXN = 1e5+10;
 9 
10 int P[MAXN];
11 int N, M;
12 
13 bool c(int d)
14 {
15     int last = 0;
16     int crt = 0;
17     for(int i = 1; i < M; i++){
18         crt = last+1;
19         while(crt < N && P[crt] - P[last] < d) crt++;
20         if(crt == N) return false;
21         last = crt;
22     }
23     return true;
24 }
25 
26 int main()
27 {
28     while(~scanf("%d %d", &N, &M) && (N+M)){
29         for(int i = 0; i < N; i++)
30             scanf("%d", &P[i]);
31         sort(P, P+N);
32         int st = 0, ed = INF;
33         int mid = 0;
34         while(ed - st > 1){
35             mid = (st+ed)/2;
36             if(c(mid)) st = mid;
37             else ed = mid;
38         }
39         printf("%d\n", st);
40     }
41     return 0;
42 }
View Code

 

 

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}(xx)2+(yy)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,n1000). 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:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <bits/stdc++.h>
 4 typedef long long ll;
 5 const int INF=0x3f3f3f3f;
 6 const int MOD=1e9+7;
 7 const int MAXN=1e3+5;
 8 
 9 using namespace std;
10 
11 struct node{
12     ll F,S;
13 }a[MAXN],b[MAXN],t;
14 ll  pos( node a,node b)
15 {
16     return ( (a.F-b.F)*(a.F-b.F)+ (a.S-b.S)*(a.S-b.S) );
17 }
18 ll R[MAXN];
19 int V[MAXN];
20 char s[500000];
21 int main()
22 {
23     int m,n;
24     while(scanf("%d%d",&m,&n)!=EOF)
25     {
26         getchar();
27         if(n==0&&m==0)
28             break;
29         memset(V,0,sizeof(V));
30         for(int i=1;i<=m;i++)
31         {
32             ll x,y;
33             scanf("%s",s);
34             sscanf(s,"(%lld,%lld)",&x,&y);
35             a[i].F=x;
36             a[i].S=y;
37         }
38         getchar();
39         for(int i=1 ; i<=n ; i++)
40         {
41             ll x,y;
42             scanf("%s",s);
43             sscanf(s,"(%lld,%lld)",&x,&y);
44             b[i].F=x;
45             b[i].S=y;
46         }
47         for(int i=1 ; i<=n ; i++)
48         scanf("%lld",&R[i]);
49 
50         for(int i=1 ; i<=n ; i++)
51         {
52             int ans=0;
53             for(int j=1 ; j<=m ; j++)
54             {
55                 ll POS=pos(b[i],a[j]);
56                 if(POS<=(R[i]*R[i]))
57                 ans++;
58             }
59             V[i]=ans;
60         }
61 
62         for(int i=1;i<=n;i++)
63             printf("%d%c",V[i],i==n?'\n':' ');
64     }
65     return 0;
66 }
View Code

 

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 N1i,jN. 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}xlowxhighy_{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:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 int n;
 6 int sum[40000];
 7 int mark[40000];
 8 void nodeupdate(int root,int l,int r,ll num)
 9 {
10 mark[root]^=1;
11 sum[root]=(r-l+1)-sum[root];
12 }
13 void pushdown(int root,int l,int r)//傳遞給兩孩子
14 {
15 if(mark[root]==0)return;
16 int mid=(l+r)/2;
17 nodeupdate(root*2,l,mid,mark[root]);
18 nodeupdate(root*2+1,mid+1,r,mark[root]);
19 mark[root]=0;
20 }
21 void update(int kl,int kr,ll num, int root=1,int l=1,int r=n)//區間[kl,kr]修改
22 {
23 if(kl<=l&&r<=kr){
24 nodeupdate(root,l,r,num);
25 return;
26 }
27 pushdown(root,l,r);
28 int mid=(l+r)/2;
29 if(kl<=mid)
30 update(kl,kr,num,root*2,l,mid);
31 if(kr>mid)
32 update(kl,kr,num,root*2+1,mid+1,r);
33 sum[root]=sum[root*2]+sum[root*2+1];
34 }
35 
36 struct node{
37 int h,a,b,flag;
38 }e[404040];
39 int cnt=0;
40 bool cmp(node a,node b){
41 if(a.h==b.h)return a.flag>b.flag;
42 return a.h<b.h;
43 }
44 int main()
45 {
46 int T,m,x1,x2,y1,y2;
47 cin>>T;
48 while(T--)
49 {
50 cin>>n>>m;
51 memset(sum,0,sizeof(sum));
52 memset(mark,0,sizeof(mark));
53 cnt=0;
54 for(int i=0;i<m;i++)
55 {
56 scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
57 e[cnt++]=node{y1,x1,x2,1};
58 e[cnt++]=node{y2,x1,x2,-1};
59 }
60 sort(e,e+cnt,cmp);
61 int ans=0;
62 for(int i=1,j=0;i<=n;i++)
63 {
64 while(j<cnt&&e[j].h<=i&&e[j].flag==1){
65 update(e[j].a,e[j].b,1);
66 j++;
67 }
68 ans+=sum[1];
69 while(j<cnt&&e[j].h<=i){
70 update(e[j].a,e[j].b,1);
71 j++;
72 }
73 }
74 cout<<ans<<endl;
75 }
76 }
View Code

 

最后自閉 A 題 和 I 題 要用到 JAVA 大數。小蒟蒻不才,來日方長。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM