Codeforces Round #249 (Div. 2)

A. Queue on Bus Stop

It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.

The bus stop queue has n groups of people. The i-th group from the beginning has ai people. Every 30 minutes an empty bus arrives at the bus stop, it can carry at most m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into the current bus, it waits for the next bus together with other groups standing after it in the queue.

Your task is to determine how many buses is needed to transport all n groups to the dacha countryside.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100). The next line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ m).

Output

Print a single integer — the number of buses that is needed to transport all n groups to the dacha countryside.

Sample test(s)
Input
4 3
2 3 2 1
Output
3
Input
3 4
1 2 1
Output
1

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int ans=0,sum=0;
    int x,n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(sum+x>m){ans++;sum=0;}
        sum+=x;
    }
    if(sum)ans++;
    cout<<ans<<endl;
    return 0;
}

B. Pasha Maximizes

Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at most k swaps.

Input

The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most k swaps.

Sample test(s)
Input
1990 1
Output
9190
Input
300 0
Output
300
Input
1034 2
Output
3104
Input
9090000078001234 6
Output
9907000008001234

思路:从头开始,每次从k个数中选最大的换到当前位置

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int k, len;
char a[N];
void boo()
{
    for (int i = 0; i < len; i++)
    {
        int p = i;
        for (int j = 1; j <= k && j + i < len; j++)
        {
            if (a[i + j] > a[p])
                p = i + j;
        }
        if (a[p] == a[i])
            continue;
        for (int j = p; j > i; j--)
            swap(a[j], a[j - 1]);
        k -= p - i;
        if (k <= 0)
            return;
    }
}

int main()
{
    scanf("%s%d", a, &k);
    len = strlen(a);
    boo();
    printf("%s\n", a);
    return 0;
}

C. Cardiogram

In this problem, your task is to use ASCII graphics to paint a cardiogram.

A cardiogram is a polyline with the following corners:

That is, a cardiogram is fully defined by a sequence of positive integers a1, a2, ..., an.

Your task is to paint a cardiogram by given sequence ai.

Input

The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 1000). It is guaranteed that the sum of all ai doesn't exceed 1000.

Output

Print max |yi - yj| lines (where yk is the y coordinate of the k-th point of the polyline), in each line print characters. Each character must equal either « / » (slash), « \ » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram.

Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.

Sample test(s)
Input
5
3 1 2 5 1
Output
      / \     
   / \ /   \    
  /       \   
 /         \  
          \ / 
Input
3
1 5 1
Output
 / \     
  \    
   \   
    \  
     \ / 

思路:找到最高点然后向两边画。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
char a[maxn][maxn];
int n,s[maxn];
int main()
{
    scanf("%d",&n);
    int r=0,c=0;
    int max1=0,sum=0,biao=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",s+i);
        if(i%2==1)sum+=s[i];
        else sum-=s[i];
        if(max1<sum){max1=sum;biao=i;}
        if(r>sum)r=sum;
        c+=s[i];
    }
    r=max1-r;
    sum=0;
    for(int i=1;i<=biao;i++)sum+=s[i];
    for(int i=0;i<=1000;i++)
        for(int j=0;j<=1000;j++)a[i][j]=' ';
    int x=0,y=sum+1;

    for(int i=biao;i>=1;i--)
    {
        for(int j=s[i];j>0;j--)
            if(i&1){x++;y--;a[x][y]='/';}
            else a[--x][--y]='\\' ;
        if(i&1)x++;
        else x--;
    }
    x=0,y=sum;
    for(int i=biao+1;i<=n;i++)
    {
        for(int j=s[i];j>=1;j--)
            if(i&1)a[--x][++y]='/';
            else a[++x][++y]='\\';
        if(i&1)x--;
        else x++;
    }

    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)cout<<a[i][j];
        cout<<endl;
    }
    return 0;
}

D. Special Grid

You are given an n × m grid, some of its nodes are black, the others are white. Moreover, it's not an ordinary grid — each unit square of the grid has painted diagonals.

The figure below is an example of such grid of size 3 × 5. Four nodes of this grid are black, the other 11 nodes are white.

Your task is to count the number of such triangles on the given grid that:

  • the corners match the white nodes, and the area is positive;
  • all sides go along the grid lines (horizontal, vertical or diagonal);
  • no side contains black nodes.
Input

The first line contains two integers n and m (2 ≤ n, m ≤ 400). Each of the following n lines contain m characters (zeros and ones) — the description of the grid. If the j-th character in the i-th line equals zero, then the node on the i-th horizontal line and on the j-th vertical line is painted white. Otherwise, the node is painted black.

The horizontal lines are numbered starting from one from top to bottom, the vertical lines are numbered starting from one from left to right.

Output

Print a single integer — the number of required triangles.

Sample test(s)
Input
3 5
10000
10010
00001
Output
20
Input
2 2
00
00
Output
4
Input
2 2
11
11
Output
0
Note

The figure below shows red and blue triangles. They are the examples of the required triangles in the first sample. One of the invalid triangles is painted green. It is invalid because not all sides go along the grid lines.


思路:

统计三角形个数。要求选定的三角形的边上不含有黑点。

明显这样图中的三角形只能是等腰直角三角形。我们可以预处理出两条对角线上的白点个数。然后枚举一个点,再递增边长,在保证两条等边不含黑点的前提下,通过判断第三条边上的白点个数来判断第三条边上是否含有黑点。

这样的三角形一共有八种。第一类是斜边在对角线上的三角形,可以枚举直角点,再递增边,判断斜边。第二类是斜边在行或列上的三角形,这样可以枚举斜边中点,沿中点递增边,然后判断两条直角边。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int maxn=405;
int n ,m;
int cnt[maxn][maxn],dl[maxn][maxn],dr[maxn][maxn];
int main()
{
   
    string a;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        for(int j=0;j<m;j++)
            cnt[i][j+1]=(a[j]=='0'?1:0);
    }
    memset(dl,0,sizeof(dl));
    memset(dr,0,sizeof(dr));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        dl[i][j]=dl[i-1][j-1]+cnt[i][j];
    for(int i=1;i<=n;i++)
        for(int j=m;j>0;j--)
        dr[i][j]=dr[i-1][j+1]+cnt[i][j];
    int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(!cnt[i][j])continue;
            for(int k=1;cnt[i-k][j]&&cnt[i][j-k];k++)
                if(dr[i][j-k]-dr[i-k-1][j+1]==k+1)ans++;
            for(int k=1;cnt[i+k][j]&&cnt[i][j+k];k++)
                if(dr[i+k][j]-dr[i-1][j+k+1]==k+1)ans++;
            for(int k=1;cnt[i-k][j]&&cnt[i][j+k];k++)
                if(dl[i][j+k]-dl[i-k-1][j-1]==k+1)ans++;
            for(int k=1;cnt[i+k][j]&&cnt[i][j-k];k++)
                if(dl[i+k][j]-dl[i-1][j-k-1]==k+1)ans++;
            for(int k=1;cnt[i-k][j]&&cnt[i+k][j];k++)
                if(dr[i][j-k]-dr[i-k-1][j+1]==k+1&&dl[i+k][j]-dl[i-1][j-k-1]==k+1)
                ans++;
            for(int k=1;cnt[i+k][j]&&cnt[i-k][j];k++)
                if(dl[i][j+k]-dl[i-k-1][j-1]==k+1&&dr[i+k][j]-dr[i-1][j+k+1]==k+1)
                ans++;
            for(int k=1;cnt[i][j-k]&&cnt[i][j+k];k++)
                if(dr[i][j-k]-dr[i-k-1][j+1]==k+1&&dl[i][j+k]-dl[i-k-1][j-1]==k+1)
                ans++;
            for(int k=1;cnt[i][j+k]&&cnt[i][j-k];k++)
                if(dr[i+k][j]-dr[i-1][j+k+1]==k+1&&dl[i+k][j]-dl[i-1][j-k-1]==k+1)
                ans++;
        }
        printf("%d\n",ans);
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值