Codeforces Round #240 (Div. 2) (ABCDE题解)

来源:互联网 发布:js url encode utf8 编辑:程序博客网 时间:2024/05/17 14:28

题目链接:http://codeforces.com/contest/415


A. Mashmokh and Lights
time limit per test:1 second
memory limit per test:256 megabytes

Mashmokh works in a factory. At the end of each day he must turn off all of the lights.

The lights on the factory are indexed from 1 to n. There are n buttons in Mashmokh's room indexed from 1 to n as well. If Mashmokh pushes button with indexi, then each light with index not less thani that is still turned on turns off.

Mashmokh is not very clever. So instead of pushing the first button he pushes some of the buttons randomly each night. He pushedm distinct buttons b1, b2, ..., bm (the buttons were pushed consecutively in the given order) this night. Now he wants to know for each light the index of the button that turned this light off. Please note that the index of buttonbi is actuallybi, noti.

Please, help Mashmokh, print these indices.

Input

The first line of the input contains two space-separated integersn and m(1 ≤ n, m ≤ 100), the number of the factory lights and the pushed buttons respectively. The next line containsm distinct space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n).

It is guaranteed that all lights will be turned off after pushing all buttons.

Output

Output n space-separated integers where thei-th number is index of the button that turns thei-th light off.

Sample test(s)
Input
5 44 3 1 2
Output
1 1 3 4 4 
Input
5 55 4 3 2 1
Output
1 2 3 4 5 
Note

In the first sample, after pressing button number 4, lights 4 and 5 are turned off and lights 1, 2 and 3 are still on. Then after pressing button number 3, light number 3 is turned off as well. Pressing button number 1 turns off lights number 1 and 2 as well so pressing button number 2 in the end has no effect. Thus button number 4 turned lights 4 and 5 off, button number 3 turned light 3 off and button number 1 turned light 1 and 2 off.

题目大意:n盏灯开始全开,m个按钮,顺序给定,每按一次按钮,这个按钮及其以后的灯若开着则关闭,现在要求每盏灯是按了哪个按钮被关上的

题目分析:直接模拟即可


#include <cstdio>#include <cstring>int a[105];struct Date{    int ans;    bool f;}d[105];int main(){    int n, m;    scanf("%d %d", &n, &m);    for(int i = 1; i <= n; i++)        d[i].f = false;    for(int i = 0; i < m; i++)    {        int t;        scanf("%d", &t);        for(int j = t; j <= n; j++)            if(!d[j].f)            {                d[j].f = true;                d[j].ans = t;            }    }    for(int i = 1; i < n; i++)        printf("%d ", d[i].ans);    printf("%d\n", d[n].ans);}



B. Mashmokh and Tokens
time limit per test:1 second
memory limit per test:256 megabytes

Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives backw tokens then he'll get dollars.

Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He hasn numbers x1, x2, ..., xn. Numberxi is the number of tokens given to each worker on thei-th day. Help him calculate for each of n days the number of tokens he can save.

Input

The first line of input contains three space-separated integersn, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input containsn space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).

Output

Output n space-separated integers. Thei-th of them is the number of tokens Mashmokh can save on thei-th day.

Sample test(s)
Input
5 1 412 6 11 9 1
Output
0 2 3 1 1 
Input
3 1 21 2 3
Output
1 0 1 
Input
1 1 11
Output
0 

题目大意:有n天,a和b是常量,场主每天给每个工人xi个物件,一个工人归还w个物件,会得到这么多钱,因为是向下取整,所以可能给不同令牌数换的钱数相同,则可以省下一些物件,现在问场主对每个工人每天可以省下多少物件

题目分析:设y=,y<=w*a/b所以w>=y*b/a,因为w为整数,因此对y*b/a像上取整就是得到钱需要的最少物件数,用xi减即可

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define ll long longusing namespace std;int const MAX = 1e5 + 5;ll x[MAX], ans[MAX];int main(){    ll n, a, b;    scanf("%I64d %I64d %I64d", &n, &a, &b);    for(int i = 0; i < n; i++)    {        scanf("%I64d", &x[i]);        ll w = x[i] * a / b;        ll num = (ll) ceil(1.0 * w / a * 1.0 * b);        ans[i] = x[i] - num;    }    for(int i = 0; i < n - 1; i++)        printf("%I64d ", ans[i]);    printf("%I64d\n", ans[n - 1]);}



C. Mashmokh and Numbers
time limit per test:1 second
memory limit per test:256 megabytes

It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.

In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbersx and y from the board, he getsgcd(x, y) points. At the beginning of the game Bimokh has zero points.

Mashmokh wants to win in the game. For this reason he wants his boss to get exactlyk points in total. But the guy doesn't know how choose the initial sequence in the right way.

Please, help him. Find n distinct integersa1, a2, ..., an such that his boss will score exactlyk points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most109.

Input

The first line of input contains two space-separated integersn, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).

Output

If such sequence doesn't exist output -1 otherwise outputn distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Sample test(s)
Input
5 2
Output
1 2 3 4 5
Input
5 3
Output
2 4 3 7 1
Input
7 2
Output
-1
Note

gcd(x, y) is greatest common divisor ofx and y.

题目大意:一开始有n个数字,一个人每次拿走前两个数可以得到他们gcd的分,此人最后得了k分,让你还原原始数列

题目分析:先判断特殊情况,n==1&&k==0时输出任意一个数即可,n / 2 > k或者k==0或者n==1时都不可能,根据数据范围可以轻松得到构造思路,构造一个最大的数,其他任意两个连续即可,因为任意两个连续的数gcd为1,我们可以通过n和k算出最大数为k - n / 2 + 1,那么要得到它,直接乘2,2 *(k - n / 2 + 1),显然这两个数的gcd就为k - n / 2 + 1,其余的从头开始连续取即可,取的时候要判断是否已经用于构造最大数。


#include <cstdio>#include <cstring>int const MAX = 1e5;int ans[MAX];int main(){    int n, k;    scanf("%d %d", &n, &k);    if(n == 1 && k == 0)    {        printf("1\n");        return 0;    }    int re;    if(n / 2 > k || k == 0 || n == 1)        printf("-1\n");    else    {        int cnt = 0;        if(n / 2 == k)        {            for(int i = 1; i <= n; i++)                ans[cnt ++] = i;        }        else        {            bool flag = false;            if(n % 2 == 1)            {                n --;                flag = true;            }            int num = k - n / 2 + 1;            ans[cnt ++] = num;            ans[cnt ++] = num * 2;            int tmp = 2;            for(int i = 1; i <= n / 2 - 1; i++)            {                while(true)                {                    if(tmp != num && tmp != num * 2 && tmp + 1 != num && tmp + 1 != num * 2)                    {                        ans[cnt ++] = tmp;                        ans[cnt ++] = tmp + 1;                        tmp += 2;                        break;                    }                    tmp += 2;                }            }            if(flag)                ans[cnt ++] = 1;        }        for(int i = 0; i < cnt - 1; i++)            printf("%d ", ans[i]);        printf("%d\n", ans[cnt - 1]);    }}



D. Mashmokh and ACM
time limit per test:1 second
memory limit per test:256 megabytes

Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

A sequence of l integersb1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is calledgood if each number divides (without a remainder) by the next number in the sequence. More formally for alli (1 ≤ i ≤ l - 1).

Given n andk find the number of good sequences of lengthk. As the answer can be rather large print it modulo1000000007 (109 + 7).

Input

The first line of input contains two space-separated integersn, k (1 ≤ n, k ≤ 2000).

Output

Output a single integer — the number of good sequences of lengthk modulo 1000000007(109 + 7).

Sample test(s)
Input
3 2
Output
5
Input
6 4
Output
39
Input
2 1
Output
2
Note

In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].


题目大意:给1-n这n个数字,从中选择k个数字组成序列b使得对所有的b(i+1)都能被bi整除

题目分析:简单的dp,dp[i][j]表示长度为i,最后一个数字为j的满足整除条件的序列个数,则

dp[i][l] = (dp[i][l] + dp[i - 1][j]),l是j的倍数,通过一层循环来枚举,最后答案为累加dp[k][1~n]


#include <cstdio>#include <cstring>int const MOD = 1e9 + 7;int const MAX = 2005;int dp[MAX][MAX];int main(){    int n, k;    scanf("%d %d", &n, &k);    for(int i = 1; i <= MAX; i++)        dp[1][i] = 1;    for(int i = 1; i <= k; i++)        for(int j = 1; j <= n; j++)            for(int l = j; l <= n; l += j)                dp[i][l] = (dp[i][l] + dp[i - 1][j]) % MOD;    int ans = 0;    for(int i = 1; i <= n; i++)        ans = (ans % MOD + dp[k][i] % MOD) % MOD;    printf("%d\n", ans);}



E. Mashmokh and Reverse Operation
time limit per test:4 seconds
memory limit per test:512 megabytes

Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

You have an array a of length2n and m queries on it. The i-th query is described by an integerqi. In order to perform thei-th query you must:

  • split the array into 2n - qi parts, where each part is a subarray consisting of2qi numbers; thej-th subarray (1 ≤ j ≤ 2n - qi) should contain the elementsa[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi];
  • reverse each of the subarrays;
  • join them into a single array in the same order (this array becomes new arraya);
  • output the number of inversions in the new a.

Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.

Input

The first line of input contains a single integern (0 ≤ n ≤ 20).

The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array.

The third line of input contains a single integerm (1 ≤ m ≤ 106).

The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Output m lines. In thei-th line print the answer (the number of inversions) for thei-th query.

Sample test(s)
Input
22 1 4 341 2 0 2
Output
0660
Input
11 230 1 1
Output
010
Note

If we reverse an array x[1], x[2], ..., x[n] it becomes new arrayy[1], y[2], ..., y[n], wherey[i] = x[n - i + 1] for eachi.

The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indicesi, j such that: i < j and x[i] > x[j].


题目大意:给一个2^n的序列,m次查询,每个查询qi,表示将序列分成2^(n - qi)份,然后把每份都逆序,组成一个新的大序列,求新的序列的逆序数,注意每次查询是对上一次查询的结果继续查询。


题目分析:绝对的好题啊,我们按2^i分层,每层的每个子序列有i个值,这样对原始序列通过DFS,类似归并排序的方法(这里就不提了)预处理出每层的正序数sum[dep][1]和逆序数sum[dep][0],查询的时候,只有qi层及以下的逆序关系会受到影响,这里不太好理解。
举个例子:比如现在序列是1 3 6 7 2 5 8 4我要分成2份则1 3 6 7和2 5 8 4再翻转合并得到7 6 3 1 4 8 5 2,显然7 6 3 1这四个数的每个数对于4 8 5 2这四个数的每个数的相对位置是没有发生变化的。
因此将qi层及以下的正逆序数直接交换,然后累加每层的逆序数即可,注意每层逆序数都是独立的,相互不影响,原因去看归并排序求逆序数的原理


#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MAXN = (1 << 20) + 5;int const MAXM = 1e6 + 6;int a[MAXN];ll sum[MAXN][2];int n, m;void DFS(int l, int r, int dep){    if(dep == 0)    {        sum[0][1] = sum[0][0] = 0;        return;    }    int mid = (l + r) >> 1;    DFS(l, mid, dep - 1);    DFS(mid + 1, r, dep - 1);    for(int i = l; i <= mid; i++)        sum[dep][0] += lower_bound(a + mid + 1, a + r + 1, a[i]) - (a + mid + 1);    for(int i = mid + 1; i <= r; i++)        sum[dep][1] += lower_bound(a + l, a + mid + 1, a[i]) - (a + l);    sort(a + l, a + r + 1);    return;}int main(){    scanf("%d", &n);    int all = (1 << n);    for(int i = 1; i <= all; i++)        scanf("%d", &a[i]);    DFS(1, all, n);    scanf("%d", &m);    int q;    for(int i = 0; i < m; i++)    {        scanf("%d", &q);        for(int j = q; j >= 0; j--)            swap(sum[j][0], sum[j][1]);        ll ans = 0;        for(int j = n; j >= 0; j--)            ans += (ll)sum[j][0];        printf("%lld\n", ans);    }}

0 0
原创粉丝点击