Codeforces Beta Round #72 (Div. 2 Only)

Codeforces Beta Round #72 (Div. 2 Only)

http://codeforces.com/contest/84

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 1000006
 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 unsigned long long ull;
14 int main(){
15     #ifndef ONLINE_JUDGE
16      //   freopen("input.txt","r",stdin);
17     #endif
18     std::ios::sync_with_stdio(false);
19     ll n;
20     cin>>n;
21     cout<<2*n-n/2<<endl;
22 }
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 1000006
 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 unsigned long long ull;
14 
15 int n;
16 ll a[100005];
17 
18 int main(){
19     #ifndef ONLINE_JUDGE
20      //   freopen("input.txt","r",stdin);
21     #endif
22     std::ios::sync_with_stdio(false);
23     cin>>n;
24     for(int i=1;i<=n;i++) cin>>a[i];
25     ll ans=n;
26     ll co=1;
27     for(int i=1;i<n;i++){
28         if(a[i]==a[i+1]){
29             co++;
30         }
31         else{
32             ans+=co*(co-1)/2;
33             co=1;
34         }
35     }
36     ans+=co*(co-1)/2;
37     cout<<ans<<endl;
38 }
View Code

C

 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 1000006
 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 unsigned long long ull;
14 
15 const int N = 10010, D = 20000;
16 int n, m, x, y, c[N], r[N], s[N];
17 vector<int> v[D * 2 + 10];
18 
19 int main(){
20     #ifndef ONLINE_JUDGE
21      //   freopen("input.txt","r",stdin);
22     #endif
23     std::ios::sync_with_stdio(false);
24     cin>>n;
25     int ans=0;
26     for (int i = 1; i <= n; i++){
27         cin>>c[i]>>r[i];
28         c[i] += D; s[i] = -1;
29         for (int j = c[i] - r[i]; j <= c[i] + r[i]; j++) v[j].push_back(i);
30     }
31     cin>>m;
32     for (int i = 1; i <= m; i++){
33         cin>>x>>y;
34         x += D;
35         for (auto j : v[x]) if (s[j] == -1)
36             if (y * y + (x - c[j]) * (x - c[j]) <= r[j] * r[j]){
37                 s[j] = i;
38                 ans++;
39             }
40     }
41     cout<<ans<<endl;
42     for (int i = 1; i <= n; i++) cout<<s[i]<<" ";
43 }
View Code

D

二分+模拟

 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 1000006
 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 unsigned long long ull;
14 
15 ll a[100005];
16 ll n,k;
17 int book[100005];
18 vector<int>ans;
19 
20 bool erfen(ll mid){
21     ll sum=0;
22     for(int i=1;i<=n;i++){
23         sum+=min(a[i],mid);
24     }
25     if(sum>=k) return true;
26     return false;
27 }
28 
29 int main(){
30     #ifndef ONLINE_JUDGE
31      //   freopen("input.txt","r",stdin);
32     #endif
33     std::ios::sync_with_stdio(false);
34     cin>>n>>k;
35     ll sum=0;
36     ll Max=0;
37     ll L,R,mid;
38     for(int i=1;i<=n;i++){
39         cin>>a[i];
40         sum+=a[i];
41         Max=max(Max,a[i]);
42     }
43     if(sum<k) cout<<-1<<endl;
44     else if(sum==k){}
45     else{
46         L=0,R=Max,mid;
47         while(L<=R){
48             mid=L+R>>1;
49             if(erfen(mid)){
50                 R=mid-1;
51             }
52             else{
53                 L=mid+1;
54             }
55         }
56         ll kk=R;
57         sum=0;
58         for(int i=1;i<=n;i++){
59             sum+=min(a[i],kk);
60             a[i]-=min(a[i],kk);
61         }
62         int flag=1;
63         for(int i=1;i<=n;i++){
64             if(sum>k&&a[i]>0){
65                 book[i]=1;
66                 ans.pb(i);
67             }
68             else if(a[i]>0){
69                 sum++;
70                 a[i]--;
71                 if(sum>k&&flag&&a[i]>=0){
72                     flag=0;
73                     ans.pb(i);
74                     book[i]=1;
75                 }
76             }
77 
78         }
79         for(int i=1;i<=n;i++){
80             if(!book[i]&&a[i]>0){
81                 book[i]=1;
82                 ans.pb(i);
83             }
84         }
85         for(int i=0;i<ans.size();i++){
86             cout<<ans[i]<<" ";
87         }
88     }
89 
90 }
View Code

E

__builtin_popcount() 函数,计算一个数转二进制之后有几个1

一道很好的最短路的题目。

题意:求S到T的最短路径,其中,路径上不同的字母不能超过K个,且答案的字典序要最小,不存在输出-1

思路:因为K小于等于4,所以可以用状压来存放不同字母的个数。在优先队列中存放距离,路径的字符串,K和该点的下表,跑一次最短路即可。

 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 1000006
 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<pair<int,string>,pii> ppp;
15 typedef unsigned long long ull;
16 
17 int n,m,k;
18 int S,T;
19 string s[55];
20 set<pii>se;
21 
22 int dist(int a,int b){
23     return abs(a/m-b/m)+abs(a%m-b%m);
24 }
25 
26 int dir[4][2]={0,1,1,0,0,-1,-1,0};
27 
28 void Dijstra(){
29     priority_queue<ppp,vector<ppp>,greater<ppp>>Q;
30     Q.push({{dist(S,T),""},{0,S}});
31     ppp ss;
32     string str;
33     int book,pos;
34     int x,y,xx,yy;
35     while(!Q.empty()){
36         ss=Q.top();
37         Q.pop();
38         str=ss.first.second;
39         book=ss.second.first;
40         pos=ss.second.second;
41         if(!se.count({book,pos})){
42             se.insert({book,pos});
43             x=pos/m,y=pos%m;
44             for(int i=0;i<4;i++){
45                 xx=x+dir[i][0];
46                 yy=y+dir[i][1];
47                 if(xx>=0&&xx<n&&yy>=00&&yy<m){
48                     if(s[xx][yy]=='T'){
49                         cout<<str<<endl;
50                         exit(0);
51                     }
52                     if(__builtin_popcount(book|(1<<(s[xx][yy]-'a')))<=k)
53                         Q.push({{dist(xx*m+yy,T)+str.length()+1,str+s[xx][yy]},{book|(1<<(s[xx][yy]-'a')),xx*m+yy}});
54                 }
55             }
56         }
57     }
58 
59 }
60 
61 int main(){
62     #ifndef ONLINE_JUDGE
63      //   freopen("input.txt","r",stdin);
64     #endif
65     std::ios::sync_with_stdio(false);
66     cin>>n>>m>>k;
67     for(int i=0;i<n;i++){
68         cin>>s[i];
69         for(int j=0;j<m;j++){
70             if(s[i][j]=='S'){
71                 S=i*m+j;
72             }
73             if(s[i][j]=='T'){
74                 T=i*m+j;
75             }
76         }
77     }
78     Dijstra();
79     cout<<-1<<endl;
80 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10489781.html