2018 Multi-University Training Contest 1

1001. Maximum Multiple

1=12+13+16=13+13+13=12+14+14 1 = 1 2 + 1 3 + 1 6 = 1 3 + 1 3 + 1 3 = 1 2 + 1 4 + 1 4 .

1002. Balanced Sequence

对于一个括号序列,令 n n 是长度,m是前缀最小值(‘(’ +1,’)’-1), sum s u m 是最后的和,那么答案就是 nsum+2m n − s u m + 2 m 。现在这个 n n 和sum都是定值,只要最大化m就好了。
每个串,把匹配后的搞掉之后,会得到一个pair ( a,b a , b ),表示 a a 个左括号接上b个右括号,把( a,b a , b )按照一定顺序排一排依次接起来就知道 m m 的最大值了。

1003. Triangle Partition

求个凸包,然后选择凸包一条边AB,然后找个和 AB A B 夹角最小的点 C C ,把ABC当做一个三角形删掉即可。这样做个 n n 次,显然这样求出来的三角形们是合法的。

1004. Distinct Values

显然可以从左往右贪心,问题转化成求一个区间里的mex,由于区间左右端点都是递增的,用个set维护下即可。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define pb push_back 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int ans[maxn];

struct node{
    int l,r;
};
node a[maxn];

bool cmp(node a,node b) {
    return a.l<b.l || (a.l==b.l&&a.r>b.r);
}

int main() {
    int cas;
    scanf("%d",&cas);
    while (cas--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=1;i<=m;i++) {
            scanf("%d%d",&a[i].l,&a[i].r);
        }
        sort(a+1,a+m+1,cmp);
        int last=0,pre=0;
        set<int> st;
        for (int i=1;i<=n;i++) st.insert(i),ans[i]=0;
        for (int i=1;i<a[1].l;i++) ans[i]=1;
        last=a[1].l-1;
        for (int i=1;i<=m;) {
            int k=i+1;
            while (a[k].r<=a[i].r&&k<=m)
                k++;
            set<int>::iterator iter = st.begin(),i1;
            if (a[i].l>last) {
                for (int j=last+1;j<a[i].l;j++) ans[j]=1;
                last=a[i].l-1;
            }
            for (int j=last+1;j<=a[i].r;j++) {
                ans[j]=*iter;
                i1=iter;iter++;
                st.erase(i1);
            }
            for (int j=pre+1;j<a[k].l;j++) if (ans[j]) st.insert(ans[j]);
            pre=a[k].l-1;
            last=a[i].r;
            i=k;
        }
        for (int i=1;i<=n;i++) {
            ans[i]=max(1,ans[i]);
            printf("%d",ans[i]);
            if (i!=n) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

1005. Maximum Weighted Matching

一个显而易见的观察是:按照操作来可以很容易进行dp。于是只要还原出操作序列即可:每次选个度数为2的点删掉,然后加入一条虚边。
dp(edge,x,y)表示处理到 edge e d g e 这条边,这条边左端点匹配状态是 x x ,右端点匹配状态是y的最大匹配和方案数。碰到重边的时候merge下即可。

1006. Period Sequence

考虑这个无限长的序列中相等的数 si1,si2,...,sik s i 1 , s i 2 , . . . , s i k ,考虑 i1modn,i2modn,...,ikmodn i 1 mod n , i 2 mod n , . . . , i k mod n ,这些构成了一条链。相邻两个位置有个 delta d e l t a ,表示下一个相同位置在 delta d e l t a 后。

然后考虑这些链,肯定是 simodn s i mod n 一样的数组成的。不妨 O(n2) O ( n 2 ) 枚举这个链的一部分,统计它的贡献。设距离上一个位置是 prev p r e v ,距离下一个位置是 next n e x t ,这时候会分成4种情况:
+ 这个prev and next都在 [a,b] [ a , b ] 内:贡献是一个等差数列乘以若干常数
+ prev部分在 [a,b] [ a , b ] 里,next全在 [a,b] [ a , b ] 内:贡献是两个等差数列相乘,然后乘以若干常数
+ prev完全在 [a,b] [ a , b ] 内,next部分在 [a,b] [ a , b ] 内:贡献是两个等差数列相乘,然后乘以若干常数
+ prev和next都部分在 [a,b] [ a , b ] 内:贡献是三个等差数列相乘,然后乘以若干常数

具体等差数列就不列出来了,有std的参考std,没有的找有std的人要。

1007. Chiaki Sequence Revisited

考虑这个数列的差分数列,除了个别项,本质就是: 1,1,0,1,1,0,0,1,1,0,1,1,0,0,0,1,1,0,... 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , . . .

可以观测到,这个序列可以这么生成:一开始只有一个 1 1 1变成 110 110 0 0 保持不变。迭代无穷多次后就是这个差分序列。

知道差分序列,可以应用阿贝尔变换,把a的前缀和搞成差分序列相关。不妨令差分序列是 da d a ,那么 a a 的前缀和

s(n)=(n1)i=0n2da(i)i=0n2da(i)i+1

利用 da d a 的分形结构,很容易算出 s(n) s ( n )

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define pb push_back 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int,int> pp;
const int maxn=105,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;
const ld pi=acos(-1.0L);
ll sum[maxn],num[maxn],base[maxn];

int main() {
    int cas;
    scanf("%d",&cas);
    num[1]=1;
    sum[1]=1;
    ll p=1;base[1]=1;
    int i;
    for (i=2;i<=100;i++) {
        num[i]=2*num[i-1]+1;
        if (num[i]>1e18) break;
        sum[i]=sum[i-1]*2ll%mod+p*(num[i-1]%mod)%mod+(p*2ll)%mod;
        sum[i]%=mod;
        p=(p*2ll)%mod;
        base[i]=p;
    }
    int q=i;
    while (cas--) {
        ll n,fun=0;
        scanf("%lld",&n);
        n--;
        ll ans=1;
        for (i=q;i;i--) {
            if (n>=num[i]) {
                n-=num[i];
                ans=(ans+sum[i])%mod;
                ans=(ans+fun*(num[i]%mod))%mod;
                fun+=base[i];
                fun%=mod;
            }
        }
        fun++;
        fun%=mod;
//      cout << n << endl;
        ans=(ans+fun*n)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

1008. RMQ Similar Sequence

RMQ-Similar实际上就是 A A B的笛卡尔树一样,这样我们就有了一个二叉树,然后可以在树上分析了。

考虑到 B B 中有元素相同的概率是0,于是可以假设 B B 里面元素互不相同,也就是说可以假定是一个排列。

显然,符合笛卡尔树的排列就是这个树的拓扑序列个数,就是n!sizei。然后显然每个排列期望的和是 n2 n 2 ,于是答案就是 n2sizei n 2 ∏ s i z e i

1009. Lyndon Substring

这边有个简单的结论,考虑 s=w1w2...wn s = w 1 w 2 . . . w n s s 的lyndon factorization,那么|wi|的最大值就是 s s 的最长lyndon substring。证明略,想知道的私信我。

于是你只要会合并两个lyndon factorization就好了。这个是个经典问题,利用lyndon factorization的单调性,只要两个二分就搞定了。

1010. Turn Off The Light

考虑只有一个起点的时候应该怎么做。令起点在p,最左边的 1 1 在位置s,最右边的 1 1 t,显然 pst p → s → t 或者 pts p → t → s 都是可以的,不妨只考虑 pst p → s → t

显然,先走到 s s ,中间该取反的就取反,然后从s走到 t t ,该取反的也取反,这些是要走的必要步数。考虑剩下来那些位置中1所在的位置为 i1,i2,...,im i 1 , i 2 , . . . , i m .我们要把这些位置的状态搞对,那么考虑相邻两两配对,来回一趟就可以把这两个都搞定。如果位置是奇数个,最后还需要和 t t 搞一下。可以证明这样是最优的。

这个过程很容易就可以推广到起点不定的情况,考虑起点p s s t的位置关系。
+ 如果 ps p ≤ s ,那么 p p s之间每个位置在过完 pst p → s → t 后都是 1 1 ,剩下部分是个定值,可以直接维护。
+ 如果spt,显然在 p p s慢慢增加到 t t 的时候,1位置的变换也是很好维护的
+ 如果 p>t p > t ,这个时候显然不如走 pts p → t → s 逻辑,所以可以在另一种情况下做。

1011. Time Zone

转换成分钟,然后随便算算就好了。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define pb push_back 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int,int> pp;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn];

int main() {
    int cas;
    scanf("%d",&cas);
    while (cas--) {
        int h,m,x,y;
        scanf("%d%d",&h,&m);
        scanf("%s",s);
        int len=strlen(s);
        x=y=0;
        int flag=0;
        for (int i=4;i<len;i++) {
            if (s[i]=='.') {
                flag=1;continue;
            }
            if (!flag) x=(x*10)+s[i]-'0'; else y=(y*10)+s[i]-'0';
        }
        if (s[3]=='-') x+=8; else if (s[3]=='+') {
            if (x>=8) x-=8; else {
                if (y==0) {
                    x-=8;
                    s[3]='-';
                    x=abs(x);
                } else {
                    y=10-y;
                    x-=7;
                    s[3]='-';
                    x=abs(x);
                }
            }
        }
        if (s[3]=='+') {
            h+=x;m+=y*6;
            if (m>=60) {
                h+=m/60;m%=60;
            }
            h%=24;
        } else {
            h-=x;m-=y*6;
            while (m<0) {
                m+=60;
                h--;
            }
            while (h<0) h+=24;
        }
        if (h==0) printf("00"); else if (h<10) printf("0%d",h); else printf("%d",h);
        printf(":");
        if (m==0) printf("00"); else if (m<10) printf("0%d",m); else printf("%d",m);
        printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值