Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)

B. Histogram Ugliness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Dormi received a histogram with n bars of height a1,a2,…,an for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.

To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:

Select an index i (1≤i≤n) where ai>0, and assign ai:=ai−1.
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.

However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.

Consider the following example where the histogram has 4 columns of heights 4,8,9,6:


The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6=18, so if Little Dormi does not modify the histogram at all, the ugliness would be 18.

However, Little Dormi can apply the operation once on column 2 and twice on column 3, resulting in a histogram with heights 4,7,7,6:


Now, as the total vertical length of the outline (red lines) is 4+3+1+6=14, the ugliness is 14+3=17 dollars. It can be proven that this is optimal.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤4⋅105).

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 4⋅105.

Output
For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.

Example
inputCopy
2
4
4 8 9 6
6
2 1 7 4 0 0
outputCopy
17
12
Note
Example 1 is the example described in the statement.

The initial histogram for example 2 is given below:


The ugliness is currently 2+1+6+3+4=16.

By applying the operation once on column 1, six times on column 3, and three times on column 4, we can end up with a histogram with heights 1,1,1,1,0,0:


The vertical length of the outline is now 1+1=2 and Little Dormi made 1+6+3=10 operations, so the final ugliness is 2+10=12, which can be proven to be optimal.


题解:贪心,注意每次贪左侧。

test 1
test 2

#include<bits/stdc++.h>
#define int long long

using namespace std;

inline int read()  {  
    int x=0,f=1;  
    char ch=getchar();  
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  
    return x*f;  
}

const int maxn=4e5+10;
int n,m,T;
int a[maxn];

void solve(){
    cin>>n;
    a[n+1]=0;
    int cnt=0,c=0;
    for(int i=1;i<=n;++i) cin>>a[i];
    int ans=0;
    for(int i=1;i<=n;++i){
        int to=min(a[i],max(a[i-1],a[i+1]));
        ans+=a[i]-to+abs(to-a[i-1]);
        a[i]=to;
    }
    cout<<ans+a[n]<<endl;
}

signed main()
{
	cin>>T;
	while(T--) solve();
	//for(;;);
	return 0;
}
C. Little Alawn's Puzzle
time limit per test2.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a 2×n grid where each row is a permutation of the numbers 1,2,3,…,n.

The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column.

Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo 109+7.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤4⋅105).

The next two lines of each test case describe the initial state of the puzzle grid. Each line will be a permutation of the numbers 1,2,3,…,n and the numbers in each column and row will be pairwise distinct.

It is guaranteed that the sum of n over all test cases does not exceed 4⋅105.

Output
For each test case output a single integer, the number of possible solved configurations of the puzzle Little Alawn can achieve from an initial solved configuration only by swapping numbers in a column. As the answer can be very large, please output it modulo 109+7.

The answer for each test case should be on a separate line.

Example
inputCopy
2
4
1 4 2 3
3 2 1 4
8
2 6 5 1 4 3 7 8
3 8 7 5 1 2 4 6
outputCopy
2
8
Note
The two possible puzzle configurations for example 1 are:

[1,4,2,3] in the first row and [3,2,1,4] in the second;
[3,2,1,4] in the first row and [1,4,2,3] in the second.

题解:并查集。考察思维。
#include<bits/stdc++.h>
#define int long long

using namespace std;

inline int read()  {  
    int x=0,f=1;  
    char ch=getchar();  
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  
    return x*f;  
}

const int maxn=4e5+10;
int T,n,a[maxn],b[maxn],p[2*maxn];
const int mod=1e9+7;

int finds(int x){
    if(x!=p[x]) p[x]=finds(p[x]);
    return p[x];
}

void uni(int a,int b){
    p[finds(a)]=finds(b);
}

void solve(){
    n=read();
    for(int i=1;i<=n;++i) a[i]=read();
    for(int i=1;i<=n;++i) b[i]=read();
    for(int i=1;i<=n;++i) p[i]=i;
    for(int i=1;i<=n;++i) uni(a[i],b[i]);
    int ans=1;
    for(int i=1;i<=n;++i){
        if(p[i]==i) ans=(ans*2)%mod;
    }
    cout<<ans<<endl;
}

signed main()
{
	T=read();
	while(T--) solve();
	//for(;;);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值