Codeforces Round #514 (Div. 2)

Codeforces Round #514 (Div. 2)

https://codeforces.com/contest/1059

A

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 10000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 int n,L,k;
23 vector<pii>ve;
24 
25 int main(){
26     std::ios::sync_with_stdio(false);
27     cin>>n>>L>>k;
28     int x,y;
29     for(int i=1;i<=n;i++){
30         cin>>x>>y;
31         ve.pb({x,y});
32     }
33     int ans=0;
34     int pos=0;
35     for(int i=0;i<ve.size();i++){
36         if(ve[i].first-pos>=k) ans+=(ve[i].first-pos)/k;
37         pos=ve[i].first+ve[i].second;
38     }
39     if(L-pos>=k){
40         ans+=(L-pos)/k;
41     }
42     cout<<ans<<endl;
43 
44 
45 }
View Code

 

B

模拟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 10000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 int n,m;
23 string str[1005];
24 bool book[1005][1005];
25 
26 void Check(int x,int y){
27     if(str[x][y]=='#'&&str[x][y+1]=='#'&&str[x][y+2]=='#'&&str[x+1][y]=='#'&&str[x+1][y+2]=='#'&&str[x+2][y]=='#'&&str[x+2][y+1]=='#'&&str[x+2][y+2]=='#'){
28        book[x][y]=1;book[x][y+1]=1;book[x][y+2]=1;book[x+1][y]=1;book[x+1][y+2]=1;book[x+2][y]=1;book[x+2][y+1]=1;book[x+2][y+2]=1;
29     }
30 }
31 
32 int main(){
33     std::ios::sync_with_stdio(false);
34     cin>>n>>m;
35     for(int i=0;i<n;i++) cin>>str[i];
36     for(int i=0;i<n-2;i++){
37         for(int j=0;j<m-2;j++){
38             Check(i,j);
39         }
40     }
41     for(int i=0;i<n;i++){
42         for(int j=0;j<m;j++){
43             if(str[i][j]=='#'){
44                 if(!book[i][j]){
45                     cout<<"NO"<<endl;
46                     return 0;
47                 }
48             }
49         }
50     }
51     cout<<"YES"<<endl;
52 }
View Code

 

C

数论

题意:给出n,有值为1-n的数字,每去掉一个值后输出剩下值的gcd,使输出的字典序最大

思路:因为相邻的两个数互质,所以要先把奇数都去掉,剩下的都是2的倍数,然后再去掉2,6,10..这样的数,使剩下的都是4的倍数,以此类推

有种情况是剩下的数为3个,比如 x,2x,3x,这样的话,gcd的值为x,x,3x

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 10000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 
23 int main(){
24     std::ios::sync_with_stdio(false);
25     int n;
26     cin>>n;
27     int nn=(n+1)/2;
28     int ans=1;
29     if(n==3){
30         cout<<ans<<" "<<ans<<" "<<ans*3<<endl;
31         return 0;
32     }
33     while(n){
34         for(int i=1;i<=nn;i++) cout<<ans<<" ";
35         ans<<=1;
36         n>>=1;
37         nn=(n+1)/2;
38         if(n==3){
39             cout<<ans<<" "<<ans<<" "<<ans*3<<endl;
40             break;
41         }
42     }
43 }
View Code

 

D

二分+几何

题意:给定n个点,求最小圆半径,使圆可以覆盖这n个点且与x轴相交

思路:二分圆的半径,遍历每个点,求该点可以被圆覆盖的区间,最后看看区间是否有交集

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 10000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<double,double>pdd;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 vector<pdd>ve;
23 
24 int sgn(double x){
25     if(x<0) return -1;
26     if(x<eps) return 0;
27     return 1;
28 }
29 
30 bool Check(double mid){
31     double L=-100000000000000000.0,R=100000000000000000.0,dist;
32     for(int i=0;i<ve.size();i++){
33         if(ve[i].second>mid*2) return false;
34         dist=sqrt(2*mid*ve[i].second-ve[i].second*ve[i].second);
35         if(L<ve[i].first-dist) L=ve[i].first-dist;
36         if(R>dist+ve[i].first) R=dist+ve[i].first;
37     }
38     return L<R;
39 }
40 
41 int main(){
42     double x,y;
43     int n;
44     scanf("%d",&n);
45     int flag1=0,flag2=0;
46     for(int i=1;i<=n;i++){
47         scanf("%lf %lf",&x,&y);
48         ve.pb({x,y});
49         if(y>0) flag1=1;
50         else if(y<0) flag2=1;
51     }
52     if(flag1&&flag2){
53         puts("-1");
54         return 0;
55     }
56     for(int i=0;i<ve.size();i++){
57         ve[i].second=fabs(ve[i].second);
58     }
59     double L=0,R=100000000000000000.0,mid;
60     for(int i=1;i<=100;i++){///因为精度问题调不好,所以直接用for循环。。。
61         mid=(L+R)/2;
62         if(Check(mid)){
63             R=mid;
64         }
65         else{
66             L=mid;
67         }
68     }
69     printf("%.7f\n",L);
70 
71 }
View Code

 

E

贪心+倍增

题意:问一棵树最少可以被分为多少条垂直路径,路径长小于等于L,点权值之和小于等于S

思路:一个个点暴力过去会超时,所以用倍增法,看看一个点最多可以向上走多少个点,符合条件就选择

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<double,double>pdd;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 ll n,L,S;
23 ll a[100005];
24 vector<int>ve[100005];
25 ll parent[maxn][35],val[maxn][35];
26 int len[maxn];
27 int ans;
28 
29 void dfs1(int now){
30     for(int i=0;i<ve[now].size();i++){
31         parent[ve[now][i]][0]=now;
32         val[ve[now][i]][0]=a[now];
33         dfs1(ve[now][i]);
34     }
35 }
36 
37 ll dfs2(int now){
38     ll sum=0;
39     for(int i=0;i<ve[now].size();i++){
40         sum=max(sum,dfs2(ve[now][i]));
41     }
42     if(sum==0){
43         ans++;
44         return len[now]-1;
45     }
46     return sum-1;
47 }
48 
49 int main(){
50     cin>>n>>L>>S;
51     for(int i=1;i<=n;i++){
52         cin>>a[i];
53         if(a[i]>S){
54             cout<<-1<<endl;
55             return 0;
56         }
57     }
58     int x;
59     for(int i=2;i<=n;i++){
60         cin>>x;
61         ve[x].pb(i);
62     }
63     dfs1(1);
64     for(int i=1;i<=18;i++){
65         for(int j=1;j<=n;j++){
66             parent[j][i]=parent[parent[j][i-1]][i-1];
67             val[j][i]=val[parent[j][i-1]][i-1]+val[j][i-1];
68         }
69     }
70     for(int i=1;i<=n;i++){
71         ll sum=a[i],now=i;
72         len[i]=1;
73         for(int j=18;j>=0&&sum<=S&&len[i]<=L;j--){
74             if(parent[now][j]&&sum+val[now][j]<=S&&len[i]+(1<<j)<=L){
75                 sum+=val[now][j];
76                 now=parent[now][j];
77                 len[i]+=(1<<j);
78             }
79         }
80     }
81     dfs2(1);
82     cout<<ans<<endl;
83 }
View Code

 

posted on 2019-03-20 12:52  Fighting_sh  阅读(133)  评论(0编辑  收藏  举报

导航