Codeforces Round #277(Div 2) A、B、C、D、E题解

时间:2023-01-05 16:01:50

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

A. Calculating Function

水题,判个奇偶即可

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin>>n)
{
if(n&)cout<<-(n+)/<<endl;
else cout<<n/<<endl;
}
return ;
}

B. OR in Matrix

先把A全部初始化为1,再把B中为0的对应的行和列在A中设为0,最后再通过得到的A来求B,看是否一致

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int a[];
vector<int>G[];
int d,n;
const ll MOD=;
ll dp[];
void dfs(int v,int u,int root,int x)
{
if((a[v]<x&&x-a[v]<=d)||(a[v]==x&&root>=v))
{
//cout<<v<<" ";
dp[v]=;
for(int i=;i<G[v].size();i++)
{
if(G[v][i]==u)continue;
dfs(G[v][i],v,root,x);
dp[v]*=(dp[G[v][i]]+);
dp[v]%=MOD;
} }
return ;
}
int main()
{
ios::sync_with_stdio(false);
//freopen("in.in","r",stdin);
while(cin>>d>>n)
{
for(int i=;i<n;i++)
{
cin>>a[i];
G[i].clear();
}
int u,v;
for(int i=;i<n-;i++)
{
cin>>u>>v;
u--,v--;
G[u].PB(v);
G[v].PB(u);
}
ll ans=;
for(int i=;i<n;i++)
{
CLR(dp,);
dfs(i,i,i,a[i]);
ans+=dp[i];
ans%=MOD;
}
cout<<ans<<endl; }
return ;
}

C. Palindrome Transformation

贪心,只需要看初始位置所在的一半字符串。取短的那一部分先改。

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
string s;
int main()
{
ios::sync_with_stdio(false);
int n,p;
cin>>n>>p>>s;
int len=n;
int ans=;
int x=(n+)/;
if(p<=x)
{
p--;
int lx=,rx=;
int i=;
while(s[i]==s[len--i]&&i<p)i++;
ans+=p-i;
lx=p-i;
int temp;
while(i<=p)
{
if(s[i]!=s[len--i])
{
temp=max(s[i],s[len--i])-min(s[i],s[len--i]);
ans+=min(temp,-temp);
}
i++;
}
i=x-;
while(s[i]==s[len--i]&&i>p)i--;
rx=i-p;
ans+=i-p;
while(i>p)
{
if(s[i]!=s[len--i])
{
temp=max(s[i],s[len--i])-min(s[i],s[len--i]);
ans+=min(temp,-temp);
}
i--;
}
ans+=min(lx,rx); }
else
{
p--;
int lx=,rx=;
int i=x;
while(s[i]==s[len--i]&&i<p)i++;
ans+=p-i;
lx=p-i;
int temp;
while(i<=p)
{
if(s[i]!=s[len--i])
{
temp=max(s[i],s[len--i])-min(s[i],s[len--i]);
ans+=min(temp,-temp);
}
i++;
}
i=len-;
while(s[i]==s[len--i]&&i>p)i--;
rx=i-p;
ans+=i-p;
while(i>p)
{
if(s[i]!=s[len--i])
{
temp=max(s[i],s[len--i])-min(s[i],s[len--i]);
ans+=min(temp,-temp);
}
i--;
}
ans+=min(lx,rx);
}
cout<<ans<<endl;
return ;
}

D. Valid Sets

As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it.

We call a set S of tree nodes valid if following conditions are satisfied:

  1. S is non-empty.
  2. S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S.
  3. Codeforces Round #277(Div 2) A、B、C、D、E题解.

Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007(109 + 7).

题目:要求所给的树中,满足任意两个节点的权值相差小于等于d的子树个数。

分析:树形dp。

为了方便计数,我采取以下方式:每次选取的子树都是保证根节点的权值为最大,当然如果单单是这样的话,权值均相等的子树是会被重复计数的,因此我在遇到权值相等的情况时,需要保证其结点的标号小于根节点。

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int a[];
vector<int>G[];
int d,n;
const ll MOD=;
ll dp[];
void dfs(int v,int u,int root,int x)
{
if((a[v]<x&&x-a[v]<=d)||(a[v]==x&&root>=v))
{
//cout<<v<<" ";
dp[v]=;
for(int i=;i<G[v].size();i++)
{
if(G[v][i]==u)continue;
dfs(G[v][i],v,root,x);
dp[v]*=(dp[G[v][i]]+);
dp[v]%=MOD;
} }
return ;
}
int main()
{
ios::sync_with_stdio(false);
//freopen("in.in","r",stdin);
while(cin>>d>>n)
{
for(int i=;i<n;i++)
{
cin>>a[i];
G[i].clear();
}
int u,v;
for(int i=;i<n-;i++)
{
cin>>u>>v;
u--,v--;
G[u].PB(v);
G[v].PB(u);
}
ll ans=;
for(int i=;i<n;i++)
{
CLR(dp,);
dfs(i,i,i,a[i]);
ans+=dp[i];
ans%=MOD;
}
cout<<ans<<endl; }
return ;
}

E. LIS of Sequence

The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.

Nam created a sequence a consisting of n (1 ≤ n ≤ 105) elements a1, a2, ..., an (1 ≤ ai ≤ 105). A subsequence ai1, ai2, ..., aik where 1 ≤ i1 < i2 < ... < ik ≤ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups:

  1. group of all i such that ai belongs to no longest increasing subsequences.
  2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
  3. group of all i such that ai belongs to every longest increasing subsequence.

Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) denoting the number of elements of sequence a.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index ibelongs to.

Sample test(s)
input
1
4
output
3
input
4
1 3 2 5
output
3223
input
4
1 5 2 3
output
3133
Note

In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.

In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.

题目:问所给的序列中,若ai不存在于任何的LIS中,则输出1,若存在于至少一个但不为全部LIS中,则输出2,若ai存在于任意一个LIS中,则输出3

分析:求出到该点(包括该点在内)的正向LIS和不包括该点的反向LIS的长度,通过此判断即可。

例如:序列   1  2  4  2  3  5  4

   l[i]     1  2  3  2  3  3  4  表示正向的到该点为止包括该点在内的LIS的长度

     r[i]     3  2  1  2  1  0  0   表示的是反向的到该点为止不包括该点的最长下降子序列的长度

如果对于某一点,若l[i]+r[i]等于LIS的长度,则其必处于某一LIS之中,否则必然不在任意一个LIS之中,可判该点为1

接下来在由于之前我记录了正向的到该点为止包括该点在内的LIS的长度,即我知道每一个处于LIS中的位置,由此,我只需判LIS中该位置的点是否唯一,若唯一,则为3,否则为2;

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int maxn=;
int a[maxn],b[maxn],dp[maxn];
int l[maxn],r[maxn];
int ans[maxn];
int deg[maxn];
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
CLR(ans,);
CLR(deg,);
for(int i=;i<n;i++)
{
cin>>a[i];
b[n--i]=-a[i];
}
fill(dp,dp+n,INF);
for(int i=;i<n;i++)
{
int temp=lower_bound(dp,dp+n,a[i])-dp;
l[i]=temp+;
dp[temp]=a[i];
}
int len=lower_bound(dp,dp+n,INF)-dp;
fill(dp,dp+n,INF);
for(int i=;i<n;i++)
{
int temp=lower_bound(dp,dp+n,b[i])-dp;
r[n--i]=temp;
dp[temp]=b[i];
}
for(int i=;i<n;i++)
{
if(l[i]+r[i]==len)
{
deg[l[i]]++;
}
else
{
ans[i]=;
}
}
for(int i=;i<n;i++)
{
if(ans[i])cout<<;
else if(deg[l[i]]>)cout<<;
else cout<<;
}
cout<<endl; }
return ;
}