CF 210 Div2

A. Levko and Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Levko loves tables that consist of n rows and n columns very much. He especially loves beautiful tables. A table is beautiful to Levko if the sum of elements in each row and column of the table equals k.

Unfortunately, he doesn't know any such table. Your task is to help him to find at least one of them.

Input

The single line contains two integers, n and k (1 ≤ n ≤ 1001 ≤ k ≤ 1000).

Output

Print any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value.

If there are multiple suitable tables, you are allowed to print any of them.

Sample test(s)
input
2 4
output
1 3
3 1
input
4 7
output
2 1 0 4
4 0 2 1
1 3 3 0
0 3 2 2
Note

In the first sample the sum in the first row is 1 + 3 = 4, in the second row — 3 + 1 = 4, in the first column — 1 + 3 = 4 and in the second column — 3 + 1 = 4. There are other beautiful tables for this sample.

In the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables that meet the statement requirements.

题意是给你n和k,让你构造一个n*n的矩阵使得该矩阵的行之和,列之和均为k。

直接YY即可。

/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 30 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
    int n , k;
    while(~scanf("%d%d" , &n , &k))
    {
        for(int i = 0; i < n ; i++)
        {
            for(int j = 0 ;j < n ; j++)
            {
                if(i == j)printf("%d%c" , k , j == n - 1 ? '\n' : ' ');
                else printf("0%c" , j == n - 1 ? '\n' : ' ');
            }
        }
    }
    return 0;
}

———————————————————————————————————————————————————

***********************************************************************************************************************************———————————————————————————————————————————————————

B. Levko and Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.

Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.

Input

The single line contains two integers n and k (1 ≤ n ≤ 1050 ≤ k ≤ n).

Output

In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist.

If there are multiple suitable permutations, you are allowed to print any of them.

Sample test(s)
input
4 2
output
2 4 3 1
input
1 1
output
-1
Note

In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.

The second sample has no beautiful permutations.

题意是给你n和k,定义good number为gcd(a[i] , i) > 1 , 请你输出一个长度为n,且含有k个good number的序列。

思路:我们可以肯定相邻两数必然互素,1与任何数互素,那么我们就把k提到首位然后按顺序输出即可。

/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 30 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[101100];
int main()
{
    int n , k;
    while(~scanf("%d%d" , &n , &k))
    {
        for(int i = 0;  i< n ; i++)a[i] = i + 1;
        if(k == n)
        {
            puts("-1");
            continue;
        }
        printf("%d" , a[n - k - 1]);
        for(int i = 0 ; i < n - k - 1 ; i++)
        {
            printf(" %d" , a[i]);
        }
        for(int i = n - k ;i < n ; i++)
        {
            printf(" %d" , a[i]);
        }
        printf("\n");
    }
    return 0;
}

———————————————————————————————————————————————————***********************************************************************************************************************************———————————————————————————————————————————————————

C. Levko and Array Recovery
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:

  1. Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri.
  2. Find the maximum of elements from li to ri. That is, calculate the value .

Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly.

Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers liri and di (1 ≤ li ≤ ri ≤ n - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers liri and mi (1 ≤ li ≤ ri ≤ n - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type.

The operations are given in the order Levko performed them on his array.

Output

In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise.

If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.

Sample test(s)
input
4 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 3 4 8
output
YES
4 7 4 7
input
4 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 3 4 13
output
NO

题意是给你一个n,m,表示对一个长度为n的序列进行m个操作,我们有两种操作:1.将区间l到r的每个数加k ; 2.输出区间最大值。接下来的m行会给你四个数x , l , r , k 。x表示第几种操作,l , r 表示区间端点,在1操作中k表示每次加的值,在2操作中表示每次取得的最大值。让你输出一个可行的序列,否则输出NO。

思路:记录每个点的add,表示加了多少,记录每个点的ub,表示每个点可行的上界,然后暴力去扫即可,最后向回扫一遍,判断是否成立。

/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 5050 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int ub[MAXN] , n , m , add[MAXN] , Q[MAXN][4];
int main()
{
    int x , l , r , k;
    while(~scanf("%d%d" , &n , &m))
    {
        for(int i = 1 ; i <= n ;  i++)
        {
            add[i] = 0;
            ub[i] = 1000000000;
        }
        clr(Q , 0);
        for(int i = 0 ; i < m ; i++)
        {
            scanf("%d%d%d%d", &x , &l , &r , &k);
            Q[i][0] = x , Q[i][1] = l , Q[i][2] = r , Q[i][3] = k;
            if(x == 1)
            {
                for(int j = l ; j <= r ; j++)
                {
                    add[j] += k;
                }
            }
            else
            {
                for(int j = l ; j <=r ; j++)
                {
                    ub[j] = min(ub[j] , k - add[j]);
                }
            }
        }
//        for(int i = 1; i <= n ; i++)cout << ub[i] << endl;
        bool fg = 0;
        for(int i = 1 ; i <= n ; i++)add[i] = 0;
        for(int i = 0 ; i < m ; i++)
        {
            l = Q[i][1] , r = Q[i][2] , k = Q[i][3];
            if(Q[i][0] == 1)
            {
                for(int j = l ; j <= r ; j++)add[j] += k;
            }
            else
            {
                int j;
                for(j = l ;j  <= r ; j++)if(ub[j] == k - add[j])break;
//                cout << j << endl;
                if(j > r)
                {
                    fg = 1;
                    break;
                }
            }
        }
        printf("%s\n" , fg ? "NO" : "YES");
        if(!fg)
        {
            for(int i = 1 ; i <= n ; i++)
            {
                printf("%d%c" , ub[i] , i == n ? '\n' : ' ');
            }
        }

    }
    return 0;
}

———————————————————————————————————————————————————***********************************************************************************************************************************———————————————————————————————————————————————————

D. Levko and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this array at all.

Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula:

The less value  c(a) is, the more beautiful the array is.

It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.

Help Levko and calculate what minimum number c(a) he can reach.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 ≤ ai ≤ 109).

Output

A single number — the minimum value of c(a) Levko can get.

Sample test(s)
input
5 2
4 7 4 7 4
output
0
input
3 1
-100 0 100
output
100
input
6 3
1 2 3 7 8 9
output
1
Note

In the first sample Levko can change the second and fourth elements and get array: 44444.

In the third sample he can get array: 123456.


题意是给你n和k,然后给你一个长度为n的序列,你可以改变k个任意一个元素的大小,请输出最小的改变后数组的最大的任意两个元素之间的差。

思路是二分。

/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 2000 + 5;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7f7f7f7f;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , b[MAXN] , sum , k  ,n;
bool ok(int d)
{
    for(int i = 1 ; i<= n; i++)
    {
        b[i] = 1;
    }
    for(int i = 2 ; i <= n ; i++)
    {
        for(int j = 1 ; j <  i ; j++)
        {
            if(abs(a[i] - a[j]) <= (LL)(i - j) * d)
            {
                b[i] = max(b[i] , b[j] + 1);
            }
        }
    }
    sum = 0;
    for(int i = 1 ; i <= n ;i++)sum = max(sum , b[i]);
    return n - sum <= k;
}
int main()
{
    int l = 0 , r = 2*1000000000;
    scanf("%d%d" , &n , &k);
    for(int i = 1 ; i <= n ; i++)scanf("%d" , &a[i]);
    while(l <= r)
    {
        int Mid = ((LL)l + r) / 2;
        if(ok(Mid))r = Mid - 1;else l = Mid + 1;
    }
    printf("%d\n" , l);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值