首页 > 代码库 > 2月每日

2月每日

2.6

Alyona and Triangles

凸包,双指针,最大面积三角形

技术分享
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <vector>
 5 using namespace std;
 6 //lrj计算几何模板
 7 
 8 typedef long long LL;
 9 const int maxn = 5555;
10 
11 struct Point
12 {
13     LL x, y;
14     Point(LL x = 0, LL y = 0) :x(x),y(y) {}
15 };
16 typedef Point Vector;
17 Vector operator + (Vector A, Vector B)    { return Vector(A.x + B.x, A.y + B.y); }
18 Vector operator - (Vector A, Vector B)    { return Vector(A.x - B.x, A.y - B.y); }
19 bool operator < (const Point& a, const Point& b)
20 { return a.x < b.x || (a.x == b.x && a.y < b.y); }
21 LL Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
22 
23 LL PolygonArea(Point* P, int n){
24     LL ans = 0;
25     for(int i = 1; i < n - 1; i++) ans += Cross(P[i] - P[0], P[i+1] - P[0]);
26     return ans;
27 }
28 
29 
30 int ConvexHull(Point* p, int n, Point* ch)
31 {
32     sort(p,p+n);
33     int m = 0;
34     for(int i = 0; i < n; ++i)
35     {
36         while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)    m--;
37         ch[m++] = p[i];
38     }
39     int k = m;
40     for(int i = n-2; i >= 0; --i)
41     {
42         while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)    m--;
43         ch[m++] = p[i];
44     }
45     if(n > 1)    m--;
46     return m;
47 }
48 
49 Point p[maxn],ch[maxn];
50 bool judge(int i,int j,int k,int tot){
51     p[0] = ch[i];p[1] = ch[j];p[2] = ch[k];
52     LL s1 = PolygonArea(p,3);
53     p[2] = ch[(k+1)%tot];
54     LL s2 = PolygonArea(p,3);
55   //  printf("=== i = %d j = %d k = %d s1 = %I64d s2 = %I64d\n",i,j,k,s1,s2);
56     return s2 > s1;
57 }
58 
59 
60 int main(){
61     int n;
62     LL S;
63     scanf("%d %I64d",&n,&S);
64     for(int i = 0;i < n;i++){
65         scanf("%I64d %I64d",&p[i].x,&p[i].y);
66     }
67     int tot = ConvexHull(p,n,ch);
68     int a,b,c;
69     LL now = 0;
70     for(int i = 0;i < tot;i++){
71         int k = (i+2) % tot;
72         for(int j = (i+1) % tot;(j+1) % tot != i;j = (j+1)% tot){
73             if(k == j) k = (k+1) % tot;
74             while((k+1) % tot != i && judge(i,j,k,tot)){
75                 k = (k+1)%tot;
76             }
77             //printf("i = %d j = %d k = %d\n",i,j,k);
78             Point tmp[5];
79             tmp[0] = ch[i];tmp[1] = ch[j];tmp[2] = ch[k];
80             if(PolygonArea(tmp,3) > now){
81                 now = PolygonArea(tmp,3);
82                 a = i;b = j;c = k;
83             }
84         }
85     }
86     Point u = ch[a] + ch[b] - ch[c]; 
87     printf("%I64d %I64d\n", u.x, u.y);
88     u = ch[c] + ch[a] - ch[b];
89     printf("%I64d %I64d\n", u.x, u.y);
90     u = ch[b] + ch[c] - ch[a];
91     printf("%I64d %I64d\n", u.x, u.y);
92     return 0;
93 }
View Code

575A - Fibonotci

线段树,矩阵

还wa着...改不动阿

2.7

Boxes

差分,-1,-1,-1,......,-1注意到每一轮有一个是加上n的

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <set>
 7 #include <map>
 8 #include <vector>
 9 #include <queue>
10 #include <bitset>
11 #include <ctime>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long ULL;
15 typedef unsigned int UI;
16 const int maxn = 1e5+5;
17 
18 int n;
19 LL a[maxn],b[maxn];
20 
21 void solve(){
22     LL sum = 0LL;
23     for(int i = 1;i <= n;i++) sum += a[i];
24     LL T = 1LL*n*(n+1)/(2LL);
25     if(sum%T){
26         puts("NO");
27         return;
28     }
29     b[n] = a[1]-a[n];
30     for(int i = 2;i <= n;i++) b[i-1] = a[i] - a[i-1];
31     LL cnt = sum/T;
32     for(int i = 1;i <= n;i++){
33         b[i] -= cnt;
34         if(b[i] > 0 || b[i]%n){
35             puts("NO");
36             return;
37         }
38     }
39     puts("YES");
40 }
41 
42 int main(){
43     while(scanf("%d",&n) != EOF){
44         for(int i = 1;i <= n;i++) scanf("%I64d",&a[i]);
45         solve();
46     }
47     return 0;
48 }
49 
50 /*w*
51 差分
52 观察每一次操作的变化
53 是正的了还加
54 n的倍数
55 
56 */
View Code

Cleaning

树形dp,dfs

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <set>
 7 #include <map>
 8 #include <vector>
 9 #include <queue>
10 #include <bitset>
11 #include <ctime>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long ULL;
15 typedef unsigned int UI;
16 const int maxn = 1e5+5;
17 
18 int n,a[maxn],deg[maxn];
19 vector<int> g[maxn];
20 int ok;
21 
22 void dfs(int u,int fa){
23     if(deg[u] == 1) return;
24     LL sum = 0LL;
25     LL mx = 0LL;
26     for(int i = 0;i < g[u].size();i++){
27         int v = g[u][i];
28         if(v == fa) continue;
29         dfs(v,u);
30         sum += a[v];
31         mx = max(mx,1LL*a[v]);
32     }
33     if(sum < a[u] || sum > 2*a[u] || mx > a[u]) {
34         ok = 0;
35         puts("NO");
36         exit(0);
37     }
38     a[u] = 2*a[u] - sum;
39 }
40 
41 void solve(){
42     int root;
43     if(n == 2){
44         if(a[1] == a[2]) puts("YES");
45         else puts("NO");
46         return;
47     }
48     for(int i = 1;i <= n;i++){
49         if(deg[i] != 1){
50             root = i;
51             break;
52         }
53     }
54     ok = 1;
55     dfs(root,-1);
56     LL ans = a[root];
57     //printf("ans = %I64d ok = %d\n",ans,ok);
58     if(ok == 1 && ans == 0) puts("YES");
59     else puts("NO");
60 }
61 
62 int main(){
63     while(scanf("%d",&n) != EOF){
64         for(int i = 1;i <= n;i++) g[i].clear();
65         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
66         int u,v;
67         memset(deg,0,sizeof(deg));
68         for(int i = 1;i < n;i++){
69             scanf("%d %d",&u,&v);
70             g[u].push_back(v);
71             g[v].push_back(u);
72             deg[u]++;
73             deg[v]++;
74         }
75         solve();
76     }
77     return 0;
78 }
79 
80 /*w*
81 
82 
83 */
View Code

2.8

441E - Valera and Number

Tree Game

2.9

Different Subsets For All Tuples

Checking cubes.

2.10

Multipliers

Eefun Guessing Words

 

2月每日