Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: 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:
- S is non-empty.
- 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.
.
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:
- group of all i such that ai belongs to no longest increasing subsequences.
- group of all i such that ai belongs to at least one but not every longest increasing subsequence.
- 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.
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).
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.
1
4
3
4
1 3 2 5
3223
4
1 5 2 3
3133
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 ;
}
Codeforces Round #277(Div 2) A、B、C、D、E题解的更多相关文章
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- Codeforces Round #277 (Div. 2) E. LIS of Sequence DP
E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...
- Codeforces Round #277 (Div. 2) D. Valid Sets 暴力
D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...
- Codeforces Round #277 (Div. 2) B. OR in Matrix 贪心
B. OR in Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/probl ...
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...
- Codeforces Round #277 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...
- codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)
题目链接:www.codeforces.com/problemset/problem/486/A题意:求表达式f(n)的值.(f(n)的表述见题目)C++代码: #include <iostre ...
- Codeforces Round #277 (Div. 2 Only)
A:SwapSort http://codeforces.com/problemset/problem/489/A 题目大意:将一个序列排序,可以交换任意两个数字,但要求交换的次数不超过n,输出任意一 ...
随机推荐
- vc2015 编译libcurl带openssl
1.先编译zlib下载地址 http://zlib.net/ 我这边vc2015编译需要配置环境变量,不知道是装了wdk的原因还是多个vc版本的原因 设置环境变量lib和include路径 INCLU ...
- 对话框(alert,prompt,confirm,showModalDialog)
alert大部分浏览器中会产生阻塞,confirm,prompt都会产生阻塞 关于showModalDialog的介绍:(转自JS中showModalDialog 详细使用) 基本介绍: ...
- php 对问卷结果进行统计
背景: 由于具体工作的原因,我做了一份纸质的问卷调查表,调查表的主要内容是让用户对10项要求(编号为A,B....)进行优先级排序,所以我得到的结果是好几百份类似于A>I>H>G&g ...
- javaweb分页思想
web上的分页分析 在web编写中的经常会遇到,数据需要分页的情况.当数据量不是很大的时候. 可以直接使用js来分页.可以很好的提高性能.简化代码.数据量大的时候.还是需要使用java的分页类 ...
- linux系统结构和系统命令初步
以上是第五课和第14课笔记 linux 基本结构: 系统构成:kernel,Modules,Lib,(Shell,Tool)系统引导:BIOS -> Bootlooder -> Kerne ...
- Is Fibo
fib = {} f = [1, 1] fib[1] = True while f[-1] < 1e10: # 不断的计算,然后加在尾部,最后比对“in” f.append(f[-1]+f[-2 ...
- Monkey and Banana(HDU 1069 动态规划)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Android平台上使用气压传感器计算海拔高度
气压传感器两年前已经开始被手机制造商运用在其设备上,但貌似没有引起开发者足够的重视.像Galaxy S III .Galaxy Note 2和小米2手机上都有,不过大家对于气压传感器比较陌生.其实大气 ...
- 转载收藏之用 - 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK
Senparc.Weixin.MP SDK已经涵盖了微信5.0的所有公共API,以及2013年10月29日升级之后大部分实用的接口. 整个项目的源代码以及已经编译好的程序集可以在这个项目中获取到:ht ...
- 一种针对虚拟机的应用软件License认证方法
由于虚拟机的硬件信息可以随意修改,使得虚拟机可能具有相同的硬件信息,在传统的应用软件License认证方式中会导致License认证漏洞.本专利提供了一种有效的解决方法. 文/王宏财 目 前,云计算的 ...