转载请注明出处: 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. .

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 ;
}

Codeforces Round #277(Div 2) A、B、C、D、E题解的更多相关文章

  1. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  2. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...

  3. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #277 (Div. 2) A. Calculating Function 水题

    A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...

  8. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  9. 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 ...

  10. Codeforces Round #277 (Div. 2 Only)

    A:SwapSort http://codeforces.com/problemset/problem/489/A 题目大意:将一个序列排序,可以交换任意两个数字,但要求交换的次数不超过n,输出任意一 ...

随机推荐

  1. requirejs的配置

    baseUrl : 所有模块的查找根路径. 当加载纯.js文件(依赖字串以/开头,或者以.js结尾,或者含有协议),不会使用baseUrl. 如未显式设置baseUrl,则默认值是加载require. ...

  2. win7 去除桌面快捷方式小箭头

    二手入了个 sony 的本子 vgn-sz780 拿手上,感觉真心不错,然后装系统,装好xp后发现怎么折腾都没声音,由于我的硬盘是SSD的虽然有点小只有60G,但是速度还是蛮不多的,于是一横心就装了 ...

  3. 转载 50种方法优化SQL Server数据库查询

    原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...

  4. mysql主从数据库复制

    http://blog.csdn.net/lgh1117/article/details/8786274 http://blog.csdn.net/libraworm/article/details/ ...

  5. POJ 2152 Fire(树形DP)

    题意: 思路:令F[i][j]表示 的最小费用.Best[i]表示以i为根节点的子树多有节点都找到负责消防站的最小费用. 好难的题... #include<algorithm> #incl ...

  6. Windows 8.1 Update1 6610 32位/64位下载、安装和新增功能简评

    今天,微软已经确认完成Windows 8.1 2014 Update RTM正式版的开发工作,累计修复99%的已知bug.随后,微软会将Win8.1首个春季更新正式版,即Win8.1 2014 Upd ...

  7. STL适配器的初步理解

    c++中的适配器有三种:容器适配器,迭代器适配器,函数适配器.下面一一介绍: 1.容器适配器:因为这些容器都是基于其他标准容器实现的所以叫做容器的适配器,具体的有stack,queue,priorit ...

  8. Windows搭建SMTP邮件服务器

    From:http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/e4cf06f5-9a36-474b-b ...

  9. POJ Oulipo (KMP)

    题目大意 : 在一个字符串中找出目标单词的个数 代码: #include<iostream> #include<cstdio> #include<cstdlib> ...

  10. Spring集成Hessian

    一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...