codeforces 803

A n*n的填满0的矩阵  让你放k个1  替代0 字典序最大  按对角线对称

从左往右从上往下直接走就可以了

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<iterator>
#include<stack>

using namespace std;

#define ll   long long
#define MAXN  110
#define inf  2000000007

int z[105][105];

int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(z,0,sizeof(z));
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(i==j)
                {
                    if(k>=1)
                    {
                        k--;
                        z[i][j]=1;
                    }
                }
                else
                {
                    if(k>=2)
                    {
                        k--;
                        k--;
                        z[i][j]=z[j][i]=1;
                    }
                }
            }
        }
        if(k>0)
            printf("-1\n");
        else
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<n;j++)
                    printf("%d ",z[i][j]);
                printf("%d\n",z[i][n]);
            }
        }
    }
    return 0;
}

N

N个数   求每个数到0的最小距离  其中至少有1个0

l[i]=l[i-1]+1   如果w[i]不是0  否则l[i]=0;

r[i]=r[i+1]+1如果w[i]不是0  否则r[i]=0;

然后取小的那个就行了

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<iterator>
#include<stack>

using namespace std;

#define ll   long long
#define MAXN  200010
#define inf  2000000007

int z[MAXN];
int l[MAXN];
int r[MAXN];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        l[0]=r[n+1]=inf;
        for(int i=1;i<=n;i++)
        {
            l[i]=r[i]=inf;
            scanf("%d",&z[i]);
        }
        for(int i=1;i<=n;i++)
        {
            if(z[i]==0)
                l[i]=0;
            else
                l[i]=l[i-1]+1;
        }
        for(int i=n;i>=1;i--)
        {
            if(z[i]==0)
                r[i]=0;
            else
                r[i]=r[i+1]+1;
        }
        for(int i=1;i<n;i++)
            printf("%d ",min(l[i],r[i]));
        printf("%d\n",min(l[n],r[n]));

    }
    return 0;
}

C  总要卡住  思维果然是弱项

k个严格递增的数和能否是n  并且最大公约数尽量大

n=d*x  x是n的因子  那么 每个数就是   d1*x   d2*x   dk*x

显然n>=(1+k)*k/2      1 ...k   那么  n*2/k/(k+1)>=x  x是因子  然后就是列举n的因子  从大到小  最后结果  x  2*x  3*x  n-前面的

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<iterator>
#include<stack>

using namespace std;

#define ll   long long
#define MAXN  200010
#define inf  2000000007
ll n,k;
int chick(ll a)
{
    if(a>n*2/k/(k+1))
        return 0;
    ll now=n;
    now=n-k*a*(k-1)/2;
    if(now<=0)
        return 0;
    if((now>(k-1)*a)&&(now%a==0))
    {
        for(int i=1;i<k;i++)
            printf("%lld ",i*a);
        printf("%lld\n",now);
         return 1;
    }
    return 0;
}

int main()
{
    scanf("%lld%lld",&n,&k);
    int i;
    double en=sqrt(n);
    for(i=1;i<=en;i++)
    {
        if(n%i==0)
        {
            if(chick(n/i))
                return 0;
        }

    }
    for(;i>=1;i--)
    {
        if(n%i==0)
        {
            if(chick(i))
                return 0;
        }
    }
    printf("-1\n");
    return 0;
}

D

一个广告  字符串  只能以 ‘-‘ ‘ ‘ 分隔   要分成n行 使得最长的最短

显然是要二分长度的  那么问题就是l  ~ l+mid-1  这段区间最右边的能分割的点在log(n)的时间里求出来    好像n也是行的nlog(n)也能过

我上了线段树  维护这个区间最右边的能分割的点

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<iterator>
#include<stack>

using namespace std;

#define ll   long long
#define MAXN  1000010
#define inf  1000000007

struct node
{
    int l,r,ind,z;
}tree[MAXN<<2];

char z[MAXN];
int len;
void Push_up(int a)
{
    if(tree[a<<1|1].z==1)
    {
        tree[a].z=1;
        tree[a].ind=tree[a<<1|1].ind;
    }
    else if(tree[a<<1].z==1)
    {
        tree[a].z=1;
        tree[a].ind=tree[a<<1].ind;
    }
    else
    {
        tree[a].z=-1;
        tree[a].ind=-1;
    }
}
void Build(int l,int r,int a)
{
    tree[a].l=l;
    tree[a].r=r;
    if(l==r)
    {
        if(z[l]==‘ ‘||z[l]==‘-‘||l==len)
        {
            tree[a].ind=l;
            tree[a].z=1;
        }
        else
        {
            tree[a].ind=-1;
            tree[a].z=-1;
        }
        return ;
    }
    int mid=(l+r)>>1;
    Build(l,mid,a<<1);
    Build(mid+1,r,a<<1|1);
    Push_up(a);
}
int Ques(int l,int r,int a1,int b1,int a)
{
    if(a1<=l&&r<=b1)
        return tree[a].ind;

    int mid=(l+r)>>1;
    int mx=-1;
    if(a1<=mid)
        mx=max(mx,Ques(l,mid,a1,b1,a<<1));
    if(b1>mid)
        mx=max(mx,Ques(mid+1,r,a1,b1,a<<1|1));
    return mx;
}
int k;
bool chick(int a)
{
    int l=0;
    int cnt=0;
    if(a==0)
        return 0;

    while(l<=len)
    {
        int r=min(l+a-1,len);
        int ind=Ques(0,len,l,r,1);
       // printf("%d\n",ind);
        if(ind==-1)
            return 0;
        l=ind+1;
        cnt++;
    }
    if(cnt<=k)
        return 1;
    return 0;
}
int main()
{
    scanf("%d",&k);
    getchar();
    gets(z);
    len=strlen(z);
    len--;
    Build(0,len,1);
    int ans=inf;
    int l=0,r=inf;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(chick(mid))
        {
             r=mid-1;
             ans=min(mid,ans);
        }
        else
            l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
}

时间: 2024-10-08 10:33:46

codeforces 803的相关文章

4.30-5.1cf补题

//yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模,就抽空把最近没做的CF题补了点..毕竟明天开始又要继续上好多课呐...Yes, I can!(? •_•)?……(I can Huá shuǐ~~) codeforces 803 A. Maximal Binary Matrix   [简单构造] 题意:n行和n列填充零矩阵. 您要将k个1放在其中,使得得到

Codeforces 803E--Roma and Poker (DP)

原题链接:http://codeforces.com/problemset/problem/803/E 题意:给一个n长度的字符串,其中'?'可以替换成'D'.'W'.'L'中的任意一种,'D'等价于0, 'W'等价于1.'L'等价于-1.输出所有'?'被替换掉后,W和L的数目之差为k,且任意一个[1, i]的子串中W和L数目之差不能等于k. 思路:用DP做.定义bool dp[i][j]代表前i个字符W和L数目之差为j, -k<=j<=k(在数组中范围为[0, 2*k]),那么当str[i]

codeforces 803B Distances to Zero

Distances to Zero 题目链接:http://codeforces.com/problemset/problem/803/B 题目大意: 给一串数字,求每个数字到离他最近数字0的距离...水题 例:2 1 0 3 0 0 3 2 4 输出:2 1 0 1 0 0 1 2 3 思路: 每次读入一个数字要么是0要么不是0 ①如不是0 到0最近距离等于左边那位距离+1 ②如是0 本身距离为0 向左循环,如果到现在输入0距离 小于 到上一个0的距离 就更新距离,到不小于位置即可 AC代码:

Educational Codeforces Round 20 C(math)

題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的gcd盡量大... 思路: 顯然題目的要求是求 n = a1*cnt + a2*cnt + a3*cnt..+ak*cnt 其中a1<a2<a3..<ak 並且要求cnt盡量大: 要使cnt盡量大,那麼顯然a1...ak要盡量小, 那麼可以令a1...ak=1, 2, 3, ..k, 令key

Codeforces 866C Gotta Go Fast - 动态规划 - 概率与期望 - 二分答案

You're trying to set the record on your favorite video game. The game consists of N levels, which must be completed sequentially in order to beat the game. You usually complete each level as fast as possible, but sometimes finish a level slower. Spec

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st