1. 程式人生 > >Codeforces Round #524 (Div. 2)(前3題題解)

Codeforces Round #524 (Div. 2)(前3題題解)

這場比賽手速場+數學場,像我這樣讀題都讀不大懂的蒟蒻表示呵呵呵。

第四題搞了半天,大概想出來了,但來不及(中途家裡網炸了)查錯,於是我交了兩次丟了100分。幸虧這次沒有掉rating。

比賽傳送門:https://codeforces.com/contest/1080。

 

A.Petya and Origami

 

題意:Petya要發出n張邀請函,每張請函需要2張紅紙,5張綠紙,8張藍紙。現在商店裡有一些不同顏色的筆記本,每本中有k張顏色相同的紙,求最少要買幾本筆記本。

這題就是一道模擬題,算出每種紙的數量,答案加上數量除以k,對於每張顏色如果還有餘數,答案加一。

 

程式碼如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(int x = l; x <= r; x++)
 7 #define repd(x, r, l) for(int x = r; x >= l; x--)
 8 #define clr(x,y) memset(x, y, sizeof(x))
 9
#define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long LL; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int
INF = 1 << 30; 21 const int p = 10000007; 22 int lowbit(int x){ return x & -x; } 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p; } return x; } 24 //head by DYH 25 26 int main(){ 27 int n, k; 28 scanf("%d%d", &n, &k); 29 int a = n * 2, b = n * 5, c = n * 8; 30 int ans = a / k + b / k + c / k; 31 if(a % k) ans++; 32 if(b % k) ans++; 33 if(c % k) ans++; 34 printf("%d\n", ans); 35 return 0; 36 }
View Code Problem-A

 

B. Margarite and the best present

 

題意:有一個序列,ai = (-1)i * i,現在有t個詢問,問你ax~y的和。

看到這題想到了小學奧數題,對於每個詢問只要求a1~y - a1~x-1就好了,至於a1~i就很好求了,當i是偶數就是i / 2, 否則是i - i / 2。搞不懂為什麼有人在luogu群上問這題是不是莫隊(蒟蒻表示壓根不會莫隊)。

 

程式碼如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(int x = l; x <= r; x++)
 7 #define repd(x, r, l) for(int x = r; x >= l; x--)
 8 #define clr(x,y) memset(x, y, sizeof(x))
 9 #define all(x) x.begin(), x.end()
10 #define pb push_back
11 #define mp make_pair
12 #define MAXN
13 #define fi first
14 #define se second
15 #define SZ(x) ((int)x.size())
16 using namespace std;
17 typedef long long LL;
18 typedef vector<int> vi;
19 typedef pair<int, int> pii;
20 const int INF = 1 << 30;
21 const int p = 10000007;
22 int lowbit(int x){ return x & -x; }
23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p; } return x; }
24 //head by DYH
25 
26 int solve(int x){
27     int res = x / 2;
28     if(x % 2 == 1) res -= x;
29     return res;
30 }
31 
32 int main(){
33     int t;
34     scanf("%d", &t);
35     rep(times, 1, t){
36         int x, y;
37         scanf("%d%d", &x, &y);
38         int ans = solve(y) - solve(x - 1);
39         printf("%d\n", ans);
40     }
41     return 0;
42 }
View Code Problem-B

 

 

C.Masha and two friends

 

題意:有一個n * m的黑白相間的棋盤,(1, 1)為白色。現在把(x1, y1)到(x2, y2)這個矩陣塗白(x1 <= x2, y1 <= y2),再把(x3, y3)到(x4, y4)這個矩陣塗黑(x3 <= x4, y3 <= y4)。現在問你有多少個黑格子和白格子。注:有多組資料。

A 、B兩題都挺水,C、D開始就是數學題了,感覺自己好菜。

要求求黑格子和白格子,只要求其中一個就好了,另一個就是拿總個數減一下,我就是求白格子的個數。

這題看上去很煩(其實打起來也很煩),但其實就是原有的個數 + 第一次塗白的黑格子個數 - 第二次塗黑的白格子個數。

第一次塗白的黑格子數就是(x1, y1)到(x2,y2)這個矩陣中原有的黑格數(然而我統計了白格子個數再拿總數減)。

統計白格子的個數方法就是總的格子個數 / 2, 如果總個數是奇數,就看左下角是否是白格子((x + y) % 2 == 0)。求黑格子數也是一樣。

求第二次塗黑的白格子個數是(x3, y3)到(x4, y4)這個矩陣原有的白格數 + 兩次塗色相交的矩陣中原來黑格子的個數(因這些格子已經被塗成白色,已經被算入答案,要減掉)。

求區間內原有的黑白格數我就不再說了,主要就是求相交的矩陣。

先判斷有無相交:

x2 < x3 || x1 > x4 || y2 < y3 || y1 > y4  

可以畫個圖方便理解。

然後就是相交的矩陣的座標。(用X1, Y1, X2, Y2表示)

X1 = max(x1, x3), X2 = min(x2, x4), Y1 = max(y1, y3), Y2 = min(y2, y4)

  

這題就這樣草率的講完了,有不理解可以看程式碼畫幾個圖模擬下。

程式碼如下

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(LL x = l; x <= r; x++)
 7 #define repd(x, r, l) for(LL x = r; x >= l; x--)
 8 #define clr(x,y) memset(x, y, sizeof(x))
 9 #define all(x) x.begin(), x.end()
10 #define pb push_back
11 #define mp make_pair
12 #define MAXN
13 #define fi first
14 #define se second
15 #define SZ(x) ((LL)x.size())
16 using namespace std;
17 typedef long long LL;
18 typedef vector<LL> vi;
19 typedef pair<LL, LL> pii;
20 const LL INF = 1 << 30;
21 const LL p = 10000007;
22 LL lowbit(LL x){ return x & -x; }
23 LL fast_power(LL a, LL b){ LL x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p; } return x; }
24 //head by DYH
25 
26 LL judge(LL x1, LL y1, LL x2, LL y2){
27     LL x = x2 - x1 + 1, y = y2 - y1 + 1;
28     LL res = x * y / 2;
29     if(x * y % 2 == 1 && (x1 + y1) % 2 == 0) res++;
30     return res;
31 }
32 
33 LL check(LL x1, LL y1, LL x2, LL y2, LL x3, LL y3, LL x4, LL y4){
34     if(x2 < x3 || x1 > x4 || y2 < y3 || y1 > y4) return 0;
35     LL X1 = max(x1, x3), X2 = min(x2, x4), Y1 = max(y1, y3), Y2 = min(y2, y4);
36     return (X2 - X1 + 1) * (Y2 - Y1 + 1) - judge(X1, Y1, X2, Y2);
37 }
38 
39 int main(){
40     LL t;
41     scanf("%I64d", &t);
42     rep(times, 1, t){
43         LL n, m;
44         scanf("%I64d%I64d", &n, &m);
45         LL x1, y1, x2, y2, x3, y3, x4, y4;
46         scanf("%lI64d%I64d%Id%I64d%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
47         LL ans = judge(1, 1, n, m);
48         ans += (x2 - x1 + 1) * (y2 - y1 + 1) - judge(x1, y1, x2, y2);
49         ans -= check(x1, y1, x2, y2, x3, y3, x4, y4) + judge(x3, y3, x4, y4);
50         printf("%I64d %I64d\n", ans, n * m - ans);
51     }
52     return 0;
53 }
View Code Problem-C

 

完成日期:2018-11-25 04:05:27