topcoder srm 695 div1 -3

1、称一个串的子串(连续一段)为$same$当且仅当这个子串所有字符都一样。子串不同当且仅当在原串中的起始位置不同。现在构造一个长度为$n$的只包含字符‘a‘,‘b‘的串$s$,使得$s$满足长度为$L$ 的$same$子串恰有$x_{L}$个.有多个答案,输出字典序最小的。

思路:首先一个长度为4的$same$子串同样可以贡献出2个长度为3的,3个长度为2的等。所以可以先进行抵消。最后就是把各个部分拼起来。至于字典序最小,就是先放长的a,再放段的b ,交替进行。

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <set>
#include <vector>
#include <time.h>
#include <queue>
#include <stack>
#include <map>
#include <assert.h>
using namespace std;

const int N=500005;

class BearPasswordLexic
{
public:

    string get(int x,int t)
    {
        string ans="";
        for(int i=1;i<=x;++i)
        {
            if(t==0) ans+=‘a‘;
            else ans+=‘b‘;
        }
        return ans;
    }

    string findPassword(vector <int> x)
    {
        vector<int> a;

        const int n=(int)x.size();
        for(int i=n-1;i>=0;--i) if(x[i])
        {
            if(x[i]<0) return "";

            for(int j=0;j<x[i];++j) a.push_back(i+1);
            for(int j=i;j>=1;--j)
            {
                x[j-1]-=(i+2-j)*x[i];
            }

        }
        sort(a.begin(),a.end());
        int i=0,j=a.size()-1;
        string ans="";
        while(i<j)
        {
            ans+=get(a[j],0);
            ans+=get(a[i],1);
            --j;
            ++i;
        }
        if(i==j) ans+=get(a[i],0);
        if(ans.size()!=n) return "";
        return ans;
    }
};

  

2、一个$n$个 顶点$m$条边的简单图,每个节点的度数最多为3。每个顶点有个权值$x_{v}$。现在对$K$条边进行更新。称一个顶点被更新当且仅当跟这个顶点相连的边中至少有一条被更新过。适当地选出$K$ 条边进行更新使得最后的被更新的顶点的权值和最大。

思路:首先,将边按照其两端顶点的权值和进行降序排序。现在,假设$K=1$,那么排序后的第一条边就是要选出的边;假设$K=2$,那么要选出的两条边一定在排序后的前六条边中。因为选出的第一条边最多影响额外的四条边,所以如果选择了第一条,那么最差的情况是影响了第2,3,4,5 条,所以第六条比之后的都要更优秀,所以不会选择后面的所有边中的边;假设第一条没有影响2,3,4,5中的,那么第二条选那条没有影响的就可以。类似$K=3$,答案一定在前十一条边中。依次类推,只需要考虑前$5(K-1)+1$条边即可。

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <set>
#include <vector>
#include <time.h>
#include <queue>
#include <stack>
#include <map>
#include <assert.h>
using namespace std;

const int N=1005;

class BearKRoads
{
    vector<pair<int,pair<int,int> > > E;
    int n,m;
    int w[N];
    int ans;
    int h[N];

    void dfs(int dep,int cur,int Max,int val)
    {
        if(val>ans) ans=val;
        if(dep==0) return;
        if(cur>=Max) return;

        for(int i=cur;i<Max;++i)
        {
            int u=E[i].second.first;
            int v=E[i].second.second;
            int tmp=val;
            if(!h[u]) tmp+=w[u];
            if(!h[v]) tmp+=w[v];
            ++h[u];
            ++h[v];
            dfs(dep-1,i+1,Max,tmp);
            --h[u];
            --h[v];
        }

    }

public:
    int maxHappy(vector<int> x,vector<int> a,vector<int> b,int K)
    {
        n=(int)x.size();
        m=(int)a.size();
        for(int i=0;i<n;++i) w[i]=x[i];
        for(int i=0;i<m;++i)
        {
            int u=a[i],v=b[i];
            E.push_back(make_pair(w[u]+w[v],make_pair(u,v)));
        }
        sort(E.begin(),E.end());
        reverse(E.begin(),E.end());
        const int M=min(5*(K-1)+1,m);
        ans=0;
        memset(h,0,sizeof(h));
        dfs(K,0,M,0);
        return ans;
    }
};

  

时间: 2024-08-24 05:08:20

topcoder srm 695 div1 -3的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

topcoder srm 738 div1 FindThePerfectTriangle(枚举)

Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle with the following properties: The coordinates of each vertex are integers between 0 and 3000, inclusive. The perimeter of the triangle must be exactly

Topcoder SRM 603 div1题解

昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点,每个节点都有一个权值,两人A和B分别进行操作,由A先手,每人可以选择一条边,将它删掉得到两个联通块.游戏不断进行下去,最后只剩下一个节点.A希望最后的节点权值尽可能大,B希望尽可能小,求这个最后的值.数据保证n<=50. 这道题真的是博弈好题啊-(感觉放到ACM很合适啊) 我们考虑第一次A会如何选

topcoder srm 320 div1

problem1 link 两个数字后面都有阶乘符号,可以抵消. import java.util.*; import java.math.*; import static java.lang.Math.*; public class ExtraordinarilyLarge { public String compare(String x, String y) { boolean both=false; while(x.endsWith("!")&&y.endsWit

topcoder srm 712 div1 -23

1.给定两个长度为$n$的数组$A,B$.有两种操作可以作用于数组$A$.第一种,将每个元素左侧的相邻数字加到其上:第二种,将每个元素右侧的相邻数字加到其上.0的左侧是$n-1$,$n-1$的右侧是0.比如1,2,3,4执行第一种操作后是5,3,5,7.给出一个不超过100的操作序列使得$A$变成$B$.$n \leq 50$. 思路:将$a_{0},a_{1},...,a_{n-1}$看做$a_{0}x^{0}+a_{1}x^{1}+...+a_{n-1}x^{n-1}$.那么第一种操作相当于

Topcoder SRM 345 Div1 250

题意:起初在(0,0),要到(x,y)去,每次只能横向或纵向移动.横向移动时,若所在直线y为偶数,那么只能往x轴正方向移动,若为奇数,只能往x轴反方向移动:纵向移动时,若所在直线x为偶数,那么只能往y轴正方向移动,若为奇数,只能往y轴反方向移动.问从起点到终点的最短距离是多少? x,y 范围是[-1e6, 1e6] 解法:一开始想到bfs(想到很自然),将(0, 0), (x, y), (x, 0), (0, y)这4个点分别周围9个点(包括自己)作为可达点.bfs处理出(0, 0)到(x, y

Topcoder SRM 653 Div1 250

Problem N个人坐成一排,属于同一个公司的人都坐在一起(挨在一起),但不知道谁属于哪个公司.现在问其中的某些人 他属于哪个公司,他的回答是Ai,Ai=0表示此人未被询问,否则表示他的回答.题目保证一定存在一种分组方案使得属于同一公司的人都坐在一起.现问方案是否唯一? Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,Ai∈[1,100] Solution 设dp[i]表示从i位置开始往后分组的情况,dp[i]=0表示无法分组,dp[i]=1表示