Codeforces Round #624 (Div. 3)

挺簡單的c++

題目連接:https://codeforces.com/contest/1311數組


A:ide

白給spa

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define pb emplace_back
 6 #define mp make_pair
 7 #define eps 1e-8
 8 #define lson (curpos<<1)
 9 #define rson (curpos<<1|1)
10 /* namespace */
11 using namespace std; 12 /* header end */
13 
14 int t; 15 
16 int main() { 17     scanf("%d", &t); 18     while (t--) { 19         int a, b, cnt = 0; 20         scanf("%d%d", &a, &b); 21         if (a == b) puts("0"); 22         else if (a < b) { 23             if ((b - a) & 1) puts("1"); 24             else puts("2"); 25         } else { 26             if ((b - a) & 1) puts("2"); 27             else puts("1"); 28  } 29  } 30     return 0; 31 }
View Code

B:code

掃n次p數組,每次都檢查a[p[i]]與a[p[i]+1]是否可交換,再檢查a數組便可blog

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define pb emplace_back
 6 #define mp make_pair
 7 #define eps 1e-8
 8 #define lson (curpos<<1)
 9 #define rson (curpos<<1|1)
10 /* namespace */
11 using namespace std; 12 /* header end */
13 
14 int t; 15 const int maxn = 110; 16 int n, m, a[maxn], p[maxn]; 17 
18 int main() { 19     scanf("%d", &t); 20     while (t--) { 21         scanf("%d%d", &n, &m); 22         for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 23         for (int i = 1; i <= m; i++) scanf("%d", &p[i]); 24         for (int cnt = 1; cnt <= n; cnt++) { 25             for (int i = 1; i <= m; i++) { 26                 if (a[p[i]] > a[p[i] + 1]) swap(a[p[i]], a[p[i] + 1]); 27  } 28  } 29         int flag = 1; 30         for (int i = 1; i < n; i++) { 31             if (a[i] > a[i + 1]) { 32                 flag = 0; 33                 break; 34  } 35  } 36         if (flag) puts("YES"); else puts("NO"); 37  } 38     return 0; 39 }
View Code

C:排序

前綴和預處理一下就行get

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define pb emplace_back
 6 #define mp make_pair
 7 #define eps 1e-8
 8 #define lson (curpos<<1)
 9 #define rson (curpos<<1|1)
10 /* namespace */
11 using namespace std; 12 /* header end */
13 
14 const int maxn = 2e5 + 10; 15 int t, n, m, p[maxn], cnt[maxn][26], ans[26]; 16 char s[maxn]; 17 
18 int main() { 19     scanf("%d", &t); 20     while (t--) { 21         scanf("%d%d", &n, &m); 22         scanf("%s", s + 1); 23         for (int i = 0; i <= n; i++) 24             for (int j = 0; j < 26; j++) 25                 cnt[i][j] = 0; 26         for (int i = 0; i < 26; i++) ans[i] = 0; 27         for (int i = 1; i <= m; i++) scanf("%d", &p[i]); 28 
29         for (int i = 1; i <= n; i++) { 30             cnt[i][s[i] - 'a']++; 31  } 32         for (int i = 1; i <= n; i++) { 33             for (int j = 0; j < 26; j++) { 34                 cnt[i][j] += cnt[i - 1][j]; 35  } 36  } 37         for (int i = 1; i <= m; i++) { 38             for (int j = 0; j < 26; j++) { 39                 ans[j] += cnt[p[i]][j]; 40  } 41  } 42         for (int i = 0; i < 26; i++) ans[i] += cnt[n][i]; 43         for (int i = 0; i < 26; i++) printf("%d ", ans[i]); 44         puts(""); 45  } 46     return 0; 47 }
View Code

D:it

比較坑的題,隊友還FST了。class

題意是給定三個正整數,知足a<=b<=c,能夠使三個數變大或變小(不能小於1),問三個數如何變化,能知足a|b且b|c的同時總變化量最小。

因爲三個數都不到1e4,因此直接想法是枚舉,但枚舉的範圍很坑。令第一個數的變化範圍爲[1,c],第二個數爲第一個數的i倍且i*a<=2*b(兩倍便可,一倍必錯),第三個數爲第二個數的j倍且j*b<=2*c。

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define pb emplace_back
 6 #define mp make_pair
 7 #define eps 1e-8
 8 #define lson (curpos<<1)
 9 #define rson (curpos<<1|1)
10 /* namespace */
11 using namespace std; 12 /* header end */
13 
14 int t, a, b, c; 15 
16 int main() { 17     scanf("%d", &t); 18     while (t--) { 19         int aa, bb, cc, ans = INT_MAX; 20         scanf("%d%d%d", &a, &b, &c); 21         for (int i = 1; i <= c; i++) { 22             int curA = i, curB = 0, curC = 0; 23             for (int j = 1; j * i <= 2 * b; j++) { 24                 curB = j * i; 25                 for (int k = 1; k * curB <= 2 * c; k++) { 26                     curC = k * curB; 27                     int tmp = abs(a - curA) + abs(b - curB) + abs(c - curC); 28                     if (tmp < ans) { 29                         ans = tmp; 30                         aa = curA, bb = curB, cc = curC; 31  } 32  } 33  } 34  } 35         printf("%d\n%d %d %d\n", ans, aa, bb, cc); 36  } 37     return 0; 38 }
View Code

E:

什麼東西,徹底沒看

F:

題意:一維座標系上給定n個整點,保證無重複點,每一個整點有一個向左或向右的速度且保持不變。定義d(i,j)爲點對i,j的最近可能距離(例如若兩個點會相遇,則d(i,j)=0),求全部點與其餘點最近可能距離之和。

很顯然,對於點對x和y,只有當x的位置在y左邊(x<y)且x速度大於y時,這兩個點纔會相遇。不然它們不只不會相遇,距離還會逐漸變大。咱們要作的是找出全部沒法相遇的點,並把它們的初始距離都加起來。

咱們考慮點的初始位置。把全部點先按位置再按速度排序。遍歷全部點,對於第i個點,要找出全部點j知足Xj<Xi且Vj<=Vi,以及全部Xj的和。顯然這裏對速度進行離散化便可。設知足條件的點的數量爲cnt,知足條件的點的位置總和爲sum,顯然對於當前點,ans+=Xi*cnt-sum,而且把個數cnt所對應的出現次數和橫座標總和都記錄下來便可。

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define pb emplace_back
 6 #define mp make_pair
 7 #define eps 1e-8
 8 #define lson (curpos<<1)
 9 #define rson (curpos<<1|1)
10 /* namespace */
11 using namespace std; 12 /* header end */
13 
14 struct FenwickTree { 15     int n; 16     vector<ll>num; 17     FenwickTree(): n(0) {} 18     FenwickTree(int _n) { 19         n = _n; 20         num.assign(n, 0); 21  } 22     void add(int i, int val) { 23         for (; i < n; i |= i + 1) num[i] += val; 24  } 25     ll sum(int i) { 26         ll ret = 0; 27         for (; i >= 0; i = (i & (i + 1)) - 1) ret += num[i]; 28         return ret; 29  } 30 }; 31 
32 int main() { 33     int n; scanf("%d", &n); 34     vector<pair<int, int>>point(n); 35     vector<int>speed; 36     for (int i = 0; i < n; i++) scanf("%d", &point[i].first); 37     for (int i = 0; i < n; i++) { 38         scanf("%d", &point[i].second); 39  speed.push_back(point[i].second); 40  } 41 
42  sort(speed.begin(), speed.end()); 43  speed.erase(unique(speed.begin(), speed.end()), speed.end()); 44  sort(point.begin(), point.end()); 45 
46     ll ans = 0; 47  FenwickTree cnt(n), sumx(n); 48     for (auto i : point) { 49         i.second = lower_bound(speed.begin(), speed.end(), i.second) - speed.begin(); 50         ans += cnt.sum(i.second) * i.first - sumx.sum(i.second); 51         cnt.add(i.second, 1); 52  sumx.add(i.second, i.first); 53  } 54     printf("%lld\n", ans); 55     return 0; 56 }
View Code
相關文章
相關標籤/搜索