Codeforces Round #365 (Div. 2) 题解

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/nike0good/article/details/52266874

Mishka and Game

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int main()
{
//  freopen("A.in","r",stdin);
//  freopen(".out","w",stdout);

    int n=read();
    int a=0,b=0;
    For(i,n) {
        int u=read(),v=read();
        a+=u>v; b+=v>u;
    }
    if (a==b) puts("Friendship is magic!^^"); else 
    if (a<b) puts("Chris"); else 
    puts("Mishka");

    return 0;
}

Mishka and trip

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (100000+10)
ll c[MAXN],s[MAXN]={0},t[MAXN]={0};
bool b[MAXN]={0};
int main()
{
//  freopen("B.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read(),k=read();
    For(i,n) c[i]=read();
    For(i,n) s[i]=s[i-1]+c[i];
    For(i,k) b[read()]=1;
    For(i,n) t[i]=t[i-1]+b[i]*c[i];
    ll ans=0;
    For(i,n) {
        if (b[i]) ans+=c[i]*(s[n]-s[i]);
        else {
            if (i==1) ans+=c[1]*(c[2]+c[n]+t[n-1]-t[2]);
            else if (i<n) ans+=c[i]*(c[i+1]+t[n]-t[i+1]);
        }
    }
    cout<<ans<<endl;

    return 0;
}

Chris and Road

注意精度

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n;
double w,u,v;
int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>w>>v>>u;
    vector<double> q;
    For(i,n) {
        double x=read();int y=read();
        if (y>0) x-=(double)y/u*v;
        q.pb(x);
    }
    sort(q.begin(),q.end());
    if (q[0]>=-1e-12||q[n-1]<=1e-12) printf("%.10lf\n",w/u);
    else {
        printf("%.10lf\n",q[n-1]/v+w/u);
    }

    return 0;
}

Mishka and Interesting sum

给一个 1 000 000长度的数列,
 1 000 000次询问,
求一个区间出现偶数(且>=2)次的数的xor和。

首先区间xor和与区间和都能用树状数组解决
奇数的xor和很好求,考虑求一个区间出现过的数的xor和
我们维护一个数列,保证到第i个元素为止最后一次出现数的位置上才有数。

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m;
#define MAXN (1000100)
ll a[MAXN]={0},s[MAXN]={0};
ll ans[MAXN]={0};
ll f[MAXN]={0};
void upd(int x,ll v){for(;x<=n;x+=x&(-x)) f[x]^=v;} 
ll get(ll x){ll v=0; for(;x;x-=x&(-x)) v^=f[x]; return v;} 
vi rr[MAXN];
int l[MAXN];
map<int, int> ls;
int main()
{
//  freopen("D.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    For(i,n) {
        a[i]=read(); ls[a[i]]=0; 
        s[i]=a[i]^s[i-1];
    }
    cin>>m;
    For(i,m) {
        int r;
        l[i]=read(); r=read();
        ans[i]=s[r]^s[l[i]-1];
        rr[r].pb(i);
    }

    For(i,n) {
        upd(i,a[i]);
        if (ls[a[i]]) upd(ls[a[i]],a[i]);
        ls[a[i]]=i;
        Rep(k,SI(rr[i])) {
            int j=rr[i][k];
            ans[j]^=get(i)^get(l[j]-1);
        }
    }
    For(i,m) {
        printf("%I64d\n",ans[i]);
    }
    return 0;
}

Mishka and Divisors

普通的01背包,但是要输出最优解时取了哪些元素,状态难以保存,
所以不用滚动数组,判断mp(f[i][h[k]],s[i][h[k]])==mp(f[i1][h[k]],s[i1][h[k]]),fs

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (15000)
vector<ll> p;
map<ll,int> h;
int sizh=0;
ll f[1010][MAXN],s[1010][MAXN];
ll a[MAXN],a2[MAXN];
ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);}
int main()
{
//  freopen("E.in","r",stdin);
//  freopen(".out","w",stdout);

    int n=read();
    ll k;
    cin>>k;
    for(ll i=1;i*i<=k;i++) {
        if (k%i==0) {
            p.pb(i);
            if (i*i!=k) p.pb(k/i);
        }
    }
    sort(p.begin(),p.end());
    int sz=p.size();
    Rep(i,sz) h[p[i]]=i,f[0][i]=s[0][i]=INF;
    For(i,n)  {
        cin>>a[i];
        a2[i]=a[i];
        a[i]=gcd(a[i],k);
    }
    f[0][h[1]]=0; s[0][h[1]]=0;
    For(i,n) {
         RepD(j,sz-1){
            ll l=p[j];
            ll t=l/gcd(l,a[i]);
            int hl=h[l],ht=h[t];
            f[i][hl]=f[i-1][hl]; s[i][hl]=s[i-1][hl];
            ll fl=f[i-1][hl],ft=f[i-1][ht],sl=s[i-1][hl],st=s[i-1][ht];
            if (fl>ft+1) {
                f[i][hl]=ft+1;
                s[i][hl]=st+a2[i];
            }
            else if (fl==ft+1&&sl>st+a2[i])
            {
                s[i][hl]=st+a2[i];
            }
        }
    }
    if (f[n][h[k]]==INF) puts("-1") ; 
    else {
        if (k==1) {
            puts("1");
            printf("%d\n",(int)(min_element(a2+1,a2+1+n)-a2));
            return 0;           
        }       
        printf("%I64d\n",f[n][h[k]]);
        ForD(i,n) {
            if (mp(f[i][h[k]],s[i][h[k]])==mp(f[i-1][h[k]],s[i-1][h[k]])) continue;
            k/=gcd(k,a[i]);
            printf("%d ",i);
        }
        cout<<endl;
    }
    return 0;
}
展开阅读全文

没有更多推荐了,返回首页