Codeforces 461C Appleman and a Sheet of Paper(模拟)

题目链接:Codeforces 461C Appleman and a Sheet of Paper

题目大意:就是一个叠被子的过程,穿插着询问一段区间上被子的单位厚度。

解题思路:用前缀和数组模拟即可。因为对于折超过一半的处理为将令一半叠上来,所以需要变量记录当前被子的正反状态。处理好下标关系即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1e6+5;

int N, Q, W[maxn];

int main () {
    scanf("%d%d", &N, &Q);
    for (int i = 1; i <= N; i++)
        W[i] = i;

    bool flag = false;
    int k, l, r;
    int n = N, bw = 0;

    while (Q--) {
        scanf("%d%d", &k, &l);

        if (k == 1) {
            if (l > n - l) {
                flag = !flag;
                l = n - l;
            }

            if (flag) {

                for (int i = 0; i < l; i++)
                    W[bw + n - l - i] += N - W[bw + n - l + i];

            } else {
                bw += l;
                for (int i = 0; i < l; i++)
                    W[bw + i] -= W[bw - i];
            }
            n -= l;

        } else {
            scanf("%d", &r);
            if (flag) {
                int tmp = n - r;
                r = n - l;
                l = tmp;
            }
            printf("%d\n", W[bw + r] - W[bw + l]);
        }
    }
    return 0;
}
时间: 2024-10-12 04:20:08

Codeforces 461C Appleman and a Sheet of Paper(模拟)的相关文章

Codeforces 461C. Appleman and a Sheet of Paper

每次只把短的部分往长的部分折叠,直接用树状数组爆搞就可以了. 每次长度都缩小一些暴力的复杂度不是太高,启发式暴力???? C. Appleman and a Sheet of Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman has a very big sheet of paper. This s

461C. Appleman and a Sheet of Paper(树状数组)

C. Appleman and a Sheet of Paper time limit per test 2 seconds memory limit per test 256 megabytes Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a she

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

题目地址:http://codeforces.com/contest/461/problem/C 题目大意:见原题. 算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记.注意细节. Code: #include <cstdio> #include <algorithm> #define N 100000 using namespace std; bool rev; int n,q,beg=1,end,typ,p,l,r,bit[N+10]; inline v

Codeforces 461B Appleman and Tree(木dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

Codechef A Game With a Sheet of Paper

Discription Yuuko and Nagi like to play the following game: Initially they take a checkered sheet of paper of the size of N x M cells. Then, one cell, let's call this cell (X, Y) is marked. Then, they take the moves alternately. A move consists of ma

Codeforces 216D Spider&#39;s Web 树状数组+模拟

题目链接:http://codeforces.com/problemset/problem/216/D 题意: 对于一个梯形区域,如果梯形左边的点数!=梯形右边的点数,那么这个梯形为红色,否则为绿色, 问: 给定的蜘蛛网中有多少个红色. 2个树状数组维护2个线段.然后暴力模拟一下,因为点数很多但需要用到的线段树只有3条,所以类似滚动数组的思想优化内存. #include<stdio.h> #include<iostream> #include<string.h> #in

Codeforces 439C Devu and Partitioning of the Array(模拟)

题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,但是只需要关注该份的和为奇数还是偶数,要求偶数堆的个数为p.输出方案. 解题思路:首先先将数组按照奇偶排序,也可以分开储存.然后先单独分k-p个奇数,然后后面的就将两个奇数当一个偶数分配,分配过程中计算是否满足,比如说奇数是否成对,以及是否分成了k堆. #include <cstdio> #include <cstring>