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,输出任意一 ...
随机推荐
- html label 标签的 for 属性
如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1 使用for属性 <label for ...
- Qt信号槽写法
1.标准写法 2.lamda表达式写法 3.地址函数指针写法
- python爬虫下载youtube单个视频
__author__ = 'Sentinel'import requestsimport reimport jsonimport sysimport shutilimport urlparse &qu ...
- key转成pvf
https://www.godaddy.com/help/converting-an-exported-pfx-code-signing-file-to-pvk-and-spc-files-using ...
- Qt5.3企业版和开源版功能区别
一: Charts Charts是QT提供的图表模块.他提供了非常简便的APIs来绘制令人惊叹的Line, Spline,Area,Scatter,Pie,Donut,Bar,Polar和Box-an ...
- uva 10401 Injured Queen Problem(dp)
题目链接:10401 - Injured Queen Problem 题目大意:给出一个字符串,要求在n * n(n为字符串的长度)的棋盘上摆放n个受伤的皇后,受伤的皇后只能攻击到同一列和它周围8个格 ...
- 【转】Android源码下载过程的一些注意事项
原文网址:http://www.360doc.com/content/14/0113/11/11948835_344809459.shtml 其它一些事项说明: 1.在源代码下载过程中,我们在源代码下 ...
- HDU 1251 字典树(前缀树)
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
- web应用的发布
将web应用打包成.war类型的...因为将其发布到服务器时,其自动解压...非常方便
- Hive 8、Hive2 beeline 和 Hive jdbc
1.Hive2 beeline Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式 启动beeline 打开两个Shell窗口,一个启动Hive2 一个beeline连接hi ...