1. 程式人生 > >KEYENCE Programming Contest 2019 Solution

KEYENCE Programming Contest 2019 Solution

A - Beginning

簽到.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a[4];
 7     while (scanf("%d", a) != EOF)
 8     {
 9         for (int i = 1; i < 4; ++i) scanf("%d", a + i);
10         sort(a, a + 4);
11         int res = 0;
12         for (int
i = 0; i < 4; ++i) res = res * 10 + a[i]; 13 puts(res == 1479 ? "YES" : "NO"); 14 } 15 return 0; 16 }
View Code

 

 

B - KEYENCE String

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 string s;
 5 
 6 string get(int pos)
 7 {
 8     string
res = ""; 9 for (int i = 0; i <= pos; ++i) res += s[i]; 10 int len = s.size(); 11 for (int i = len - (7 - pos - 1); i < len; ++i) res += s[i]; 12 return res; 13 } 14 15 bool work() 16 { 17 for (int i = 0; i < 7; ++i) 18 { 19 string tmp = get(i); 20 if (tmp == "
keyence") return 1; 21 } 22 return 0; 23 } 24 25 int main() 26 { 27 while (cin >> s) puts(work() ? "YES" : "NO"); 28 return 0; 29 }
View Code

 

C - Exam and Wizard

Solved.

題意:

給出些個數字$A_i, 和 B_i$

要求構造$C_i 使得 C_i >= B_i 並且 \sum A_i = \sum C_i$

並且使得變動的數字個數最少

思路:

先弄出不足的部分,然後取差值最大的$A_i > B_i$ 的部分用堆維護,每次取最大的貢獻出來補充

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define N 100010
 5 #define ll long long
 6 int n, a[N], b[N];
 7 
 8 int main()
 9 {
10     while (scanf("%d", &n) != EOF)
11     {
12         for (int i = 1; i <= n; ++i) scanf("%d", a + i);
13         for (int i = 1; i <= n; ++i) scanf("%d", b + i);
14         ll need = 0;
15         priority_queue <int, vector <int>, less <int> > pq;
16         int res = 0; 
17         for (int i = 1; i <= n; ++i) 
18         {
19             if (b[i] > a[i]) need += b[i] - a[i], ++res;
20             else if (b[i] < a[i]) pq.push(a[i] - b[i]);
21         }
22         while (!pq.empty() && need > 0)
23         {
24             int top = pq.top(); pq.pop();
25             need -= top; ++res;    
26         }
27         if (need > 0) puts("-1");
28         else printf("%d\n", res);
29     }
30     return 0;
31 }
View Code

 

E - Connecting Cities

Unsolved.

題意:

$n個城市,兩個城市之間的邊權是 |i - j| \cdot D + A_i + A_j$

求最小生成樹