Codeforces Beta Round # 10 B. Cinema Cashier (Fenwick tree)

Subject to the effect:

n k * k wave of people to the cinema.

To try to take the middle, sitting forward.



Direct enumeration, greedy, sit sit sit from the recent local centers.



#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 1000005
#define lowbit(x) (x&(-x))

using namespace std;

struct BIT
{
    int sum;
    void init(){sum=0;}
}bit[105][105];
int q,n;
int Sum(int row,int x)
{
    int ret=0;
    for(int i=x;i>=1;i-=lowbit(i))
        ret+=bit[row][i].sum;
    return ret;
}
int query(int row,int l,int r)
{
    return Sum(row,r)-Sum(row,l-1);
}
void update(int row,int x)
{
    for(int i=x;i<=n;i+=lowbit(i))
        bit[row][i].sum++;
}
int cal(int s,int e)
{
    return (s+e)*(e-s+1)/2;
}
int main()
{
    scanf("%d%d",&q,&n);
    int cen=n/2+1;

    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
        {
            bit[i][j].init();
        }
    for(int i=1;i<=q;i++)
    {
        int num;
        scanf("%d",&num);
        int ansr=-1,ansc=-1;
        int min_val=0x3f3f3f3f;

        for(int r=1;r<=n;r++)
        {
            for(int c=1;c+num-1<=n;c++)
            {
                if(query(r,c,c+num-1)==0)
                {
                    int tmp;
                    if(c>=cen)
                    {
                        tmp=cal(c,c+num-1)-cen*num+abs(r-cen)*num;
                    }
                    else if(c+num-1<=cen)
                    {
                        tmp=cen*num-cal(c,c+num-1)+abs(r-cen)*num;
                    }
                    else
                    {
                        tmp=abs(r-cen)*num+cal(cen,c+num-1)-(c+num-cen)*cen+cen*(cen-c)-cal(c,cen-1);
                    }
                    if(tmp<min_val)
                    {
                        min_val=tmp;
                        ansr=r;
                        ansc=c;
                    }
                }
            }
        }
        if(min_val!=0x3f3f3f3f)
        {
            printf("%d %d %d\n",ansr,ansc,ansc+num-1);
                for(int j=ansc;j<=ansc+num-1;j++)
                    update(ansr,j);
        }
        else puts("-1");
    }
    return 0;
}


Intelligent Recommendation

Codeforces Beta Round #1-B. Spreadsheets

Codeforces Beta Round #1-B. Spreadsheets In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — numb...

Codeforces Beta Round #10 D. LCIS

Topic links: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limit per test:256 megabytes Problem Description This problem differs from one which was on the o...

Educational Codeforces Round 10 D. Nested Segments offline Fenwick tree discrete

D. Nested Segments Topic connection: http://www.codeforces.com/contest/652/problem/D Description You are given n segments on a line. There are no ends of some segments that coincide. For each segment ...

Palisection (Codeforces Beta Round # 17E + palindrome tree)

Topic Link Portal The meaning of problems Give you a string, you have to ask how much of the palindrome string intersect. Thinking Due to the positive answers do not very good count, then we consider ...

CodeForces Beta Round # 75 (Div. 1 Only) B. Queue (Weight Dip Tree)

   Given n number, for each number to find the rightmost number than him, how much is the distance between these two numbers, if not output-1 Using the right line segment tree puts the posit...

More Recommendation

Codeforces Beta Round #2 B. The least round way

Fortunately, this problem ~ ~ 2B Subject to the effect: Given a matrix. Go left on the bottom right, you can only turn right or laid down. Each path has a grid number. The number obtained by multiplyi...

Codeforces Beta Round #2 B. The least round way dp

B. The least round way Topic connection: http://www.codeforces.com/contest/2/problem/B Description There is a square matrix n × n, consisting of non-negative integer numbers. You s...

Codeforces Beta Round # 2 B. The least round way (dynamic programming)

B. The least round way time limit per test:2 seconds memory limit per test:64 megabytes There is a square matrix n × n, consisting of non-negative integer numbers. You should ...

Codeforces Beta Round #33 (Codeforces format) B. String Problem(Floyd)

Add link description The meaning of the question: Given two initial strings s and t, and then given n transformations, it means that it takes the cost of w to change the letter 1 to the letter 2. Ask ...

Codeforces Beta Round #33 (Codeforces format) B. String Problem (floyd)

Topic link Topic Given two strings, n a b x are given, indicating that the character a can become b, and each time it costs x. Ask what is the minimum cost to make these two strings the same. If it ca...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top