Educational Codeforces Round 41 B、C、D

http://codeforces.com/contest/961

B题 可以将长度为k的连续区间转化成1 求最大和

解析 简单尺取

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <map>
11 #include <vector>
12 #include <set>
13 #include <utility>
14 using namespace std;
15 const int maxn = 2e5+50,maxm = 1e4+10;
16 const int inf = 0x3f3f3f3f,mod = 998244353;
17 const double epx = 1e-6;
18 typedef long long ll;
19 const ll INF = 1e18;
20 const double pi = acos(-1.0);
21 int n,m,k;
22 int a[maxn],b[maxn];
23 int main()
24 {
25     cin>>n>>k;
26     for(int i=1;i<=n;i++)
27     {
28         cin>>a[i];
29     }
30     int sum=0;
31     for(int i=1;i<=n;i++)
32     {
33         cin>>b[i];
34         if(b[i])
35             sum+=a[i];
36     }
37     int maxx=-1;
38     int l=1,r=1,temp=0;
39     while(l<=n-k+1)
40     {
41         if(r-l<k)
42         {
43             if(b[r]==0)
44                 temp+=a[r];
45             r++;
46         }
47         else
48         {
49             //cout<<l<<" "<<r<<" "<<temp<<endl;
50             maxx=max(maxx,temp+sum);
51             if(b[l]==0)
52                 sum-=a[l];
53             l++;
54         }
55     }
56     cout<<maxx<<endl;
57 }
View Code

C题 棋盘分成了大小相同的四块 替换最少的方块  组成一个合格(0,1不相邻)的棋盘

j解析 对于一个合格的棋盘只有两种情况 左上角1 左上角0  四块拼成大棋盘的组合情况有24种 数组范围不大直接每种都跑一边就好了 

其实直接枚举6种就好了哪两块左上角为1    C(4,2) 

  1 #include <stdio.h>
  2 #include <math.h>
  3 #include <string.h>
  4 #include <stdlib.h>
  5 #include <iostream>
  6 #include <sstream>
  7 #include <algorithm>
  8 #include <string>
  9 #include <queue>
 10 #include <map>
 11 #include <vector>
 12 #include <set>
 13 #include <utility>
 14 using namespace std;
 15 const int maxn = 1e2+50,maxm = 1e4+10;
 16 const int inf = 0x3f3f3f3f,mod = 998244353;
 17 const double epx = 1e-6;
 18 const double pi = acos(-1.0);
 19 typedef long long ll;
 20 const ll INF = 1e18;
 21 int n,m,k,sum;
 22 char a[10][maxn][maxn];
 23 char g[2*maxn][2*maxn];
 24 int vis[2*maxn][2*maxn];
 25 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 26 int aa[100],xu[100][100],cnt=0;
 27 void dfs(int cur,int n)
 28 {
 29     for(int i=0;i<n;i++)
 30         aa[i]=i+cur;
 31     do{
 32         for(int i=0;i<n;i++)
 33             xu[cnt][i]=aa[i];
 34         cnt++;
 35     }while(next_permutation(aa,aa+4));
 36 }
 37 void DFS(int x,int y,char z)
 38 {
 39     vis[x][y]=1;
 40     if((x+y)%2==0&&g[x][y]!=z)
 41         sum++;
 42     else if((x+y)%2==1&&g[x][y]==z)
 43         sum++;
 44     for(int i=0;i<4;i++)
 45     {
 46         int x1=x+dir[i][0];
 47         int y1=y+dir[i][1];
 48         if(vis[x1][y1]==0&&x1>=0&&x1<=2*n-1&&y1>=0&&y1<=2*n-1)
 49         {
 50             DFS(x1,y1,z);
 51         }
 52     }
 53 }
 54 int main()
 55 {
 56     cin>>n;
 57     dfs(1,4);
 58     int minn=inf;
 59     for(int i=1;i<=4;i++)
 60     {
 61         for(int j=0;j<n;j++)
 62         {
 63             cin>>a[i][j];
 64         }
 65     }
 66     for(int i=0;i<cnt;i++)
 67     {
 68         for(int j=0;j<4;j++)
 69         {
 70             if(j==0)
 71             {
 72                 for(int k=0;k<n;k++)
 73                 {
 74                     for(int l=0;l<n;l++)
 75                     {
 76                         g[k][l]=a[xu[i][j]][k][l];
 77                     }
 78                 }
 79             }
 80             else if(j==1)
 81             {
 82                 for(int k=0;k<n;k++)
 83                 {
 84                     for(int l=0;l<n;l++)
 85                     {
 86                         g[k][l+n]=a[xu[i][j]][k][l];
 87                     }
 88                 }
 89             }
 90             else if(j==2)
 91             {
 92                 for(int k=0;k<n;k++)
 93                 {
 94                     for(int l=0;l<n;l++)
 95                     {
 96                         g[k+n][l]=a[xu[i][j]][k][l];
 97                     }
 98                 }
 99             }
100             else if(j==3)
101             {
102                 for(int k=0;k<n;k++)
103                 {
104                     for(int l=0;l<n;l++)
105                     {
106                         g[k+n][l+n]=a[xu[i][j]][k][l];
107                     }
108                 }
109             }
110         }
111         memset(vis,0,sizeof(vis));
112         sum=0;DFS(0,0,'0');
113         minn=min(minn,sum);
114         memset(vis,0,sizeof(vis));
115         sum=0;DFS(0,0,'1');
116         minn=min(minn,sum);
117     }
118     cout<<minn<<endl;
119 }
View Code

D题 n个点 问是否都在   某一条或某两条直线上 

解析  n<3 yes   n>=3 对于任意三个点a,b,c   若a,b在一条直线上L,除去在L的点,如果剩下的的点在一条直线上 有解

             若a,c在一条直线上L,除去在L的点,如果剩下的的点在一条直线上 有解

             若b,c在一条直线上L,除去在L的点,如果剩下的的点在一条直线上 有解

             若三种情况都没有解  则无解

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <map>
11 #include <vector>
12 #include <set>
13 #include <utility>
14 using namespace std;
15 const int maxn = 1e6+50,inf = 0x3f3f3f3f,mod = 998244353;
16 const double epx = 1e-6;
17 const double pi = acos(-1.0);
18 typedef long long ll;
19 const ll INF = 1e18;
20 int n,m,k;
21 typedef pair<int,int> pii;
22 pii p[maxn];
23 int vis[maxn];
24 pii operator -(const pii &a,const pii &b)
25 {
26     return {a.first-b.first,a.second-b.second};
27 }
28 ll cross(const pii &a,const pii &b)
29 {
30     return a.first*1ll*b.second-a.second*1ll*b.first;
31 }
32 bool check()
33 {
34     int i1=-1,i2=-1;
35     for(int i=0;i<n;i++)
36     {
37         if(vis[i])
38             continue;
39         if(i1==-1)
40             i1=i;
41         else if(i2==-1)
42             i2=i;
43     }
44     if(i2==-1)
45         return true;
46     for(int i=0;i<n;i++)
47     {
48         if(vis[i])
49             continue;
50         if(cross(p[i2]-p[i1],p[i]-p[i1])!=0)
51             return false;
52     }
53     return true;
54 }
55 bool check2(pii a,pii b)
56 {
57     memset(vis,0,sizeof(vis));
58     for(int i=0;i<n;i++)
59     {
60         if(cross(b-a,p[i]-a)==0)
61             vis[i]=1;
62     }
63     return check();
64 }
65 int main()
66 {
67     cin>>n;
68     for(int i=0;i<n;i++)
69         cin>>p[i].first>>p[i].second;
70     if(n<=2)
71         cout<<"YES"<<endl;
72     else
73     {
74         if(check2(p[0],p[1])||check2(p[0],p[2])||check2(p[1],p[2]))
75             cout<<"YES"<<endl;
76         else
77             cout<<"NO"<<endl;
78     }
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/stranger-/p/8781617.html