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,输出任意一 ...
随机推荐
- oracle RAC调整数据文件大小并移动表到指定的表空间
一.Oracle RAC 调整表空间数据文件大小 1.先查找出表空间对应的数据文件路径: select file_name,tablespace_name from dba_data_files ; ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- Python Set集合,函数,深入拷贝,浅入拷贝,文件处理
1.Set基本数据类型 a.set集合,是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set o ...
- 几种TCP连接中出现RST的情况
http://blog.chinaunix.net/uid-24517549-id-3991141.html http://blog.chinaunix.net/uid-24517549-id-399 ...
- Qt Creator快捷键大全,附快捷键配置方法
一.快捷键配置方法: 进入“工具->选项->环境->键盘”即可配置快捷键. 二.常用默认快捷键: 编号 快捷键 功能 1 Esc 切换到代码编辑状态 2 F1 查看帮助( ...
- Android开源中国客户端学习 (自定义View)左右滑动控件ScrollLayout
左右滑动的控件我们使用的也是非常多了,但是基本上都是使用的viewpager 等 android基础的控件,那么我们有么有考虑过查看他的源码进行定制呢?当然,如果你自我感觉非常好的话可以自己定制一个, ...
- 《数据通信与网络》笔记--SSL/TLS
上次简单地介绍了IP层的安全,今天来介绍下在传输层提供安全性最主要的2个协议:安全套接字层(SSL)协议和传输 层安全(TLS)协议.TLS实际上就是SSL的IETF版本. 1.SSL服务 设计安全套 ...
- spring 4 泛型注入
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...
- Linux shell编程02 shell程序的执行 及文件权限
第一个shell脚本 1. shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2. ...
- 【转】vlc android 代码编译
转自:http://blog.csdn.net/asircao/article/details/7734201 系统:ubuntu12.04代码:git://git.videolan.org/vlc- ...