codeforce Educational div2(赛后补题)B、C、D、E

B. Minimum Ternary String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').

You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).

For example, for string "010210" we can perform the following moves:

  • "010210" "100210";
  • "010210" "001210";
  • "010210" "010120";
  • "010210" "010201".

Note than you cannot swap "02" "20" and vice versa. You cannot perform any other operations with the given string excluding described above.

You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).

String aa is lexicographically less than string bb (if strings aa and bb have the same length) if there exists some position ii (1i|a|1≤i≤|a|, where |s||s| is the length of the string ss) such that for every j<ij<i holds aj=bjaj=bj, and ai<biai<bi.

Input

The first line of the input contains the string ss consisting only of characters '0', '1' and '2', its length is between 11 and 105105 (inclusive).

Output

Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).

Examples
Input
Copy
100210
Output
Copy
001120
Input
Copy
11222121
Output
Copy
11112222
Input
Copy
20
Output
Copy
20

这道题呵呵。昨晚把我做的心态爆炸,一直用的是模拟,因为一眼看过去没啥规律。
没想到还真是规律题,昨天模拟是先贪心一遍对于每个贪心再对于已经贪心部分继续贪心,但是1201就没办法处理。

单独讨论到最后实在心态崩了。早上看AC代码...我去这都是啥。
首先可以判断1在序列中是可以随意滑动的,如果没有2 ,一定是0000001111111,如果有2 ,肯定让2尽可能的往后,

对于2后面的1,如果是211,就是112,如果是201,就是120,因为1可以往前移,但是0由于和2 由差距,所以没办法移动到前面,把1去除之后就是0的位置。同理有两个2,200021111,第二个2后的1可以先类似的移动到第一个2 后面,然后再移动到第一个2 前面,以此类推,第一2后面把1取走之后就是最后位置,第一个2前面就只有0和1,又是第一种情况。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std; 
char A[100050];
string s; 
int main()
{
   int x=0,y=0,zero[100050];
   memset(zero,0,sizeof(zero));
   cin>>A;
   int j=0;
   int len=strlen(A);
   for(int i=0;i<len;i++)
   {
   	  if(A[i]=='1')y++;
   	  if(A[i]=='0'&&j==0)x++;
   	  if(A[i]=='2')
   	  {
  	   	 ++j;
      }
      if(A[i]=='0'&&j>0)
      {
      	 zero[j]++;
      }
   }
    for(int i=1;i<=x;i++)
    cout<<"0";
    for(int i=1;i<=y;i++)
    cout<<"1";
    if(j!=0)
    for(int i=1;i<=j;i++)
    {
    	 cout<<"2";
    	 for(int k=1;k<=zero[i];k++)
    	 cout<<"0";
    }
} 
C. Annoying Present
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice got an array of length nn as a birthday present once again! This is the third year in a row!

And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.

Bob has chosen mm changes of the following form. For some integer numbers xx and dd, he chooses an arbitrary position ii (1in1≤i≤n) and for every j[1,n]j∈[1,n] adds x+ddist(i,j)x+d⋅dist(i,j) to the value of the jj-th cell. dist(i,j)dist(i,j) is the distance between positions ii and jj (i.e. dist(i,j)=|ij|dist(i,j)=|i−j|, where |x||x| is an absolute value of xx).

For example, if Alice currently has an array [2,1,2,2][2,1,2,2] and Bob chooses position 33 for x=1x=−1 and d=2d=2 then the array will become [21+22, 11+21, 21+20, 21+21][2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1] = [5,2,1,3][5,2,1,3]. Note that Bob can't choose position ii outside of the array (that is, smaller than 11 or greater than nn).

Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.

What is the maximum arithmetic mean value Bob can achieve?

Input

The first line contains two integers nn and mm (1n,m1051≤n,m≤105) — the number of elements of the array and the number of changes.

Each of the next mm lines contains two integers xixi and didi (103xi,di103−103≤xi,di≤103) — the parameters for the ii-th change.

Output

Print the maximal average arithmetic mean of the elements Bob can achieve.

Your answer is considered correct if its absolute or relative error doesn't exceed 10610−6.

Examples
Input
Copy
2 3
-1 3
0 0
-1 -4
Output
Copy
-2.500000000000000
Input
Copy
3 2
0 2
5 0
Output
Copy
7.000000000000000

半天也没读懂意思,最后才知道应该是操作的算术平均值,因为并没有给数组大小。
对于|i-j|*d的操作,易知一定在端点和中间位置去最大最小,d>0取最大,d<0取最小,(我分不清哪个最大最小, 举例子计算就可以判断的出来)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std; 
char A[100050];
string s; 
int main()
{
    long long n,m;
	long long ans=0;
	cin>>n>>m;
	long long x,y;
	long long sum1=n*(n-1)/2;
	long long w=(n+1)/2;
	long long sum2=(w==1?0:w*(w-1)/2)+(n-w+1)*(n-w)/2;

	for(int i=1;i<=m;i++)
	{
		cin>>x;
		ans+=x*n;
		cin>>y;
		if(y>0)
		ans+=y*sum1;
		else
		ans+=y*sum2;
	} 
	printf("%.15f",(double)ans/n);
} 
/**题目精度真的把人搞死了。


D. Relatively Prime Graph
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)E(v,u)∈E  GCD(v,u)=1GCD(v,u)=1 (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices vv and uu then the value of GCD(v,u)GCD(v,u) doesn't matter. The vertices are numbered from 11 to |V||V|.

Construct a relatively prime graph with nn vertices and mm edges such that it is connected and it contains neither self-loops nor multiple edges.

If there exists no valid graph with the given number of vertices and edges then output "Impossible".

If there are multiple answers then print any of them.

Input

The only line contains two integers nn and mm (1n,m1051≤n,m≤105) — the number of vertices and the number of edges.

Output

If there exists no valid graph with the given number of vertices and edges then output "Impossible".

Otherwise print the answer in the following format:

The first line should contain the word "Possible".

The ii-th of the next mm lines should contain the ii-th edge (vi,ui)(vi,ui) of the resulting graph (1vi,uin,viui1≤vi,ui≤n,vi≠ui). For each pair (v,u)(v,u) there can be no more pairs (v,u)(v,u) or (u,v)(u,v). The vertices are numbered from 11 to nn.

If there are multiple answers then print any of them.

Examples
Input
Copy
5 6
Output
Copy
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
Copy
6 12
Output
Copy
Impossible
Note

Here is the representation of the graph from the first example: 

这道题就是保证做出来的图是连通图而且图中的边顶点互质,然后呢我们从1开始,可以选择n-1条边,选完了就连通了,所以只要m>=n-1,就一定可以保证连通,然后从2开始一步一步到N判断是否互质,1开始到N,2得时候不用考虑1,因为1->2是否互质已经在1得时候判断,又因为是无向图,所以2的时候就不需要再判断,纯暴力没试过不知道会不会T。
我加了一点点优化,奇数没办法优化,偶数得话和偶数一定不是互质,所以可以+=2跳过偶数判断互质。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std; 
char A[100050];
string s; 
struct node
{
	int a;
	int b;
}NO[100050];
int main()
{
	 int n,m; 
     cin>>n>>m;
     int ans=0;
	 int j=0;
	 for(int i=1;i<=n-1;i++)
     {
     	if(i==1)
     	for(int k=2;k<=n;k++)
     	{
	       NO[++j].a=1;
		   NO[j].b=k;
		   ans++;
		   if(ans==m)break;	
        }
        else
		{
			if(i%2==1)
			for(int k=i+1;k<=n;k++)
			{
				if(__gcd(i,k)==1)
				{
					NO[++j].a=i;
					NO[j].b=k;
					ans++;
					if(ans==m)break;
				}
			}
			else
			{
				for(int k=i+1;k<=n;k+=2)
				{
					if(__gcd(i,k)==1)
					{
						NO[++j].a=i;
						NO[j].b=k;
						ans++;
						if(ans==m)break;
					}
				}
			}
		} 
        if(ans==m)break;
     }
     if(ans==m&&m>=n-1)
     {
     	cout<<"Possible"<<endl;
     	for(int x=1;x<=m;x++)
     	{
	     	cout<<NO[x].a<<" "<<NO[x].b<<endl;
	     }
     }
     else cout<<"Impossible";
} 

//**话说基本暴力都能过真的是...

这次血亏...早知道C题D题这么简单...就不会在B题上卡到心态爆炸了。
有些不甘心看到E题有七八百人过了就来尝试一下,然后我就花了两个小时,四十分钟推这个公式,主要可能自己也不够认真,但是这次推公式让我思维算是有了一次进步(知道以后还有这种套路),我推出来是2 6 16 40,但是我没考虑为啥题目说的2^n-1,因为最后一个休不休息都没有用,所以不考虑,这个时候推出来应该是1 3 8 20

1+2=3 1+3+2^2=8 1+3+8+2^3=20答案就已经揭晓了。
E. Intercity Travelling
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.

The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is nn km. Let's say that Moscow is situated at the point with coordinate 00 km, and Saratov — at coordinate nn km.

Driving for a long time may be really difficult. Formally, if Leha has already covered ii kilometers since he stopped to have a rest, he considers the difficulty of covering (i+1)(i+1)-th kilometer as ai+1ai+1. It is guaranteed that for every i[1,n1]i∈[1,n−1] aiai+1ai≤ai+1. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.

Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 11 to n1n−1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a1a1, the kilometer after it — difficulty a2a2, and so on.

For example, if n=5n=5 and there is a rest site in coordinate 22, the difficulty of journey will be 2a1+2a2+a32a1+2a2+a3: the first kilometer will have difficulty a1a1, the second one — a2a2, then Leha will have a rest, and the third kilometer will have difficulty a1a1, the fourth — a2a2, and the last one — a3a3. Another example: if n=7n=7 and there are rest sites in coordinates 11 and 55, the difficulty of Leha's journey is 3a1+2a2+a3+a43a1+2a2+a3+a4.

Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2n12n−1 different distributions of rest sites (two distributions are different if there exists some point xx such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate pp — the expected value of difficulty of his journey.

Obviously, p2n1p⋅2n−1 is an integer number. You have to calculate it modulo 998244353998244353.

Input

The first line contains one number nn (1n1061≤n≤106) — the distance from Moscow to Saratov.

The second line contains nn integer numbers a1a1, a2a2, ..., anan (1a1a2an1061≤a1≤a2≤⋯≤an≤106), where aiai is the difficulty of ii-th kilometer after Leha has rested.

Output

Print one number — p2n1p⋅2n−1, taken modulo 998244353998244353.

Examples
Input
Copy
2
1 2
Output
Copy
5
Input
Copy
4
1 3 3 7
Output
Copy
60

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std; 
long long A[1000050];
string s; 
long long quickpow(long long n, long long base) {
    long long res = 1;
    while(n) {
        if(n & 1) {
            res = res * base % 998244353;
        }
        n >>= 1;
        base = base * base % 998244353;
    }
    return res;
}
struct node
{
	int a;
	int b;
}NO[100050];
int main()
{
    long long n,tap;
    long long ans=0;
    long long bns=0,cns=0;
    long long pow0=1;
	ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

	cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>A[i];
    }
    for(int i=n;i>=1;i--)
    {
   	   tap=A[i];
       cns=(bns+pow0)%998244353;
       cns=(cns+998244353)%998244353;
       bns+=cns;
       bns%=998244353;
	   ans+=(cns*tap);
	   ans%=998244353;
	   pow0*=2;
	   pow0%=998244353;
    }
    cout<<ans;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值