Codeforces Beta Round #57 (Div. 2)

Codeforces Beta Round #57 (Div. 2)

http://codeforces.com/contest/61

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 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 string s1,s2;
14 
15 int main(){
16     #ifndef ONLINE_JUDGE
17         freopen("input.txt","r",stdin);
18     #endif
19     std::ios::sync_with_stdio(false);
20     cin>>s1>>s2;
21     rep(i,0,s1.length()){
22         if(s1[i]==s2[i]){
23             cout<<0;
24         }
25         else cout<<1;
26     }
27 }
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 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 string s[3];
14 map<string,int>mp;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18         freopen("input.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     string str;
22     rep(i,0,3){
23          s[i]="";
24          cin>>str;
25          rep(j,0,str.length()){
26             if(str[j]>='A'&&str[j]<='Z') str[j]+=32;
27             if(str[j]>='a'&&str[j]<='z') s[i]+=str[j];
28          }
29     }
30     int n;
31     cin>>n;
32     string ss;
33     mp[s[0]]=1,mp[s[1]]=1,mp[s[2]]=1;
34     mp[s[0]+s[1]]=1;mp[s[0]+s[2]]=1;mp[s[1]+s[0]]=1;mp[s[1]+s[2]]=1;mp[s[2]+s[0]]=1;mp[s[2]+s[1]]=1;
35     mp[s[0]+s[1]+s[2]]=1;mp[s[0]+s[2]+s[1]]=1;mp[s[1]+s[0]+s[2]]=1;mp[s[1]+s[2]+s[0]]=1;mp[s[2]+s[1]+s[0]]=1;mp[s[2]+s[0]+s[1]]=1;
36     while(n--){
37         cin>>str;
38         ss="";
39         rep(i,0,str.length()){
40             if(str[i]>='A'&&str[i]<='Z') str[i]+=32;
41             if(str[i]>='a'&&str[i]<='z') ss+=str[i];
42         }
43         if(mp[ss]) cout<<"ACC"<<endl;
44         else cout<<"WA"<<endl;
45     }
46 }
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 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 string a[13]={"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
14 int v[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
15 string val[3005];
16 
17 string roman(int n) {
18     string ret="";
19     for(int i=0;i<13;i++)
20         while(n>=v[i]) {
21             n-=v[i];
22             ret += a[i];
23         }
24     return ret;
25 }
26 void pre() {
27     for(int i=1;i<=3000;i++)
28         val[i] = roman(i);
29 }
30 
31 char c[2000], B[10];
32 int A;
33 
34 int main(){
35     #ifndef ONLINE_JUDGE
36        // freopen("input.txt","r",stdin);
37     #endif
38     std::ios::sync_with_stdio(false);
39     pre();
40     cin>>A>>B>>c;
41     long long v = 0;
42     for(int i=0;c[i];i++) {
43         v=v*A + (c[i]>='A'? c[i]-'A'+10: c[i]-'0');
44     }
45     if(B[0]=='R') cout<<val[v].c_str()<<endl;
46     else {
47         char d[2000];
48         int dn=0, base = atoi(B);
49         if(v==0) {
50             d[dn++]=0;
51         }
52         while(v>0) {
53             d[dn++]=v%base;
54             v/=base;
55         }
56         while(dn-->0) cout<<char(d[dn]>=10? d[dn]-10+'A': d[dn]+'0');
57         cout<<endl;
58     }
59 }
View Code

D

通过模拟找规律可以发现,最短距离为所有的距离之和*2减去根结点下的最长链

 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 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 int n;
14 vector<pair<int,ll> >ve[100005];
15 
16 int dfs(int pos,int pre){
17     ll Max=0;
18     ll tmp;
19     ll now;
20     rep(i,0,ve[pos].size()){
21         if(ve[pos][i].first!=pre){
22             now=ve[pos][i].second;
23             tmp=dfs(ve[pos][i].first,pos);
24             if(tmp+now>Max){
25                 Max=tmp+now;
26             }
27         }
28     }
29     return Max;
30 }
31 
32 int main(){
33     #ifndef ONLINE_JUDGE
34        // freopen("input.txt","r",stdin);
35     #endif
36     std::ios::sync_with_stdio(false);
37     cin>>n;
38     int u,v;
39     ll w;
40     ll sum=0;
41     rep(i,1,n){
42         cin>>u>>v>>w;
43         sum+=w;
44         ve[u].pb(make_pair(v,w));
45         ve[v].pb(make_pair(u,w));
46     }
47     sum+=sum;
48     int Max=0;
49     ll tmp;
50     rep(i,0,ve[1].size()){
51         tmp=dfs(ve[1][i].first,1);
52         if(tmp+ve[1][i].second>Max){
53             Max=tmp+ve[1][i].second;
54         }
55     }
56     cout<<sum-Max<<endl;
57 }
View Code

E

找三元祖,问前面比a[i]大且后面比a[i]小的三元组有多少个

直接上树状树组,离散化后枚举每个点,找出前面比它大的数,然后乘上后面比它小的数即可,会爆int

 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 rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 int tree[maxn];
14 
15 int lowbit(int x){
16     return x&(-x);
17 }
18 
19 void update(int x){
20     while(x<maxn){
21         tree[x]+=1;
22         x+=lowbit(x);
23     }
24 }
25 
26 int query(int x){
27     int ans=0;
28     while(x){
29         ans+=tree[x];
30         x-=lowbit(x);
31     }
32     return ans;
33 }
34 
35 int a[maxn];
36 int n;
37 vector<int>ve;
38 
39 int getpos(int x){
40     return lower_bound(ve.begin(),ve.end(),x)-ve.begin()+1;
41 }
42 
43 ll pre[maxn];
44 
45 int main(){
46     #ifndef ONLINE_JUDGE
47        // freopen("input.txt","r",stdin);
48     #endif
49     std::ios::sync_with_stdio(false);
50     cin>>n;
51     rep(i,1,n+1){
52         cin>>a[i];
53         ve.pb(a[i]);
54     }
55     sort(ve.begin(),ve.end());
56     ve.erase(unique(ve.begin(),ve.end()),ve.end());
57     int pos;
58     update(getpos(a[1]));
59     ll ans=0;
60     rep(i,2,n){
61         pos=getpos(a[i]);
62         pre[i]=query(maxn-1)-query(pos);
63      //   cout<<query(maxn-1)<<" "<<query(pos)<<endl;
64         update(pos);
65         ///
66       //  cout<<pre[i]<<" "<<((n-i)-(n-pos-pre[i]))<<endl;
67         ans+=pre[i]*(-i+pos+pre[i]);
68     }
69     cout<<ans<<endl;
70 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10444610.html