Codeforces Round #277 (Div. 2)
整理上次写的题目:
A:
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
The single line contains the positive integer n (1 ≤ n ≤ 10^15).
题目简洁。可以看出规律。。。分下奇偶就可以了。
B:
1 second
256 megabytes
standard input
standard output
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
where
is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print mrows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
题目比较烦。
但是我们可以得出这样一个规律:先预设定所有数为1,然后按得出的答案去把数变为0.然后验证是否满足答案。。
明白这一点 就简单了。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
typedef long long ll;
using namespace std; int a[][];
int b[][];
int main()
{
int m,n;
cin>>m>>n;
for (int i=;i<=m;i++)
for (int j=;j<=n;j++) b[i][j]=;
for (int i=;i<=m;i++)
for (int j=;j<=n;j++)
cin>>a[i][j]; for (int i=;i<=m;i++)
for (int j=;j<=n;j++)
if (a[i][j]==){
for (int k=;k<=n;k++)
b[i][k]=;
for (int k=;k<=m;k++)
b[k][j]=;
} for (int i=;i<=m;i++)
for (int j=;j<=n;j++)
if (a[i][j]==)
{
int tmp=;
for (int k=;k<=n;k++)
tmp|=b[i][k];
for (int k=;k<=m;k++)
tmp|=b[k][j];
if (tmp==)
{
cout<<"NO";
return ;
}
} cout<<"YES"<<endl;
for (int i=;i<=m;i++){
for (int j=;j<=n;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
return ;
}
同样很繁琐的题目,但是我们可以贪心之。
贪心策略:1,先计算字符串的一半值。。。比如abcbcc
对应的值应当是211112.我们发现对半分的字符串所需最少的步数是对称的。还有我们要计算出最少步数。这里具体见代码。
然后当p在左半部分是就只做左半部分。同理右半部份。。。我们不可能p<=(n+1)/2,去做右半部份。
然后抽象认为这样的题目:数组为1 2 3 0 2的数组。当p在某个位置时。求全部变为0最少的步数。。。
这里用贪心或者啥的。我用了一个比较巧的办法。
然后因为写的太繁琐。露了条件。。。一直WA。。真是菜
#include<bits/stdc++.h>
using namespace std;
#define N 123456
string s;
int a[N];
int n,p; int main()
{
cin>>n>>p;
cin>>s;
for (int i=;i<s.size();i++){
int tmp1=abs(s[i]-s[n-i-]);
int tmp2;
if (s[i]>s[n-i-]) tmp2=abs(s[i]--s[n-i-]);
else tmp2=abs(s[n-i-]-s[i]-);
a[i+]=min(tmp1,tmp2);
} int mid=(n+)/;
int ans=0x3f3f3f; if (p<=mid)
{
int ans1=;
int tmpp=p;
for (int i=;i<=mid;i++)
if (a[i])
{
ans1+=a[i];
ans1+=abs(tmpp-i);
tmpp=i;
}
ans=min(ans1,ans); ans1=;
tmpp=p;
for(int i=mid;i>=;i--)
if (a[i])
{
ans1+=a[i];
ans1+=abs(tmpp-i);
tmpp=i;
}
ans=min(ans1,ans);
} else
{ int ans1=;
int tmpp=p;
for (int i=mid+;i<=n;i++)
if (a[i])
{
ans1+=a[i];
ans1+=abs(tmpp-i);
tmpp=i;
}
ans=min(ans1,ans);
ans1=;
tmpp=p;
for(int i=n;i>mid;i--)
if (a[i])
{
ans1+=a[i];
ans1+=abs(tmpp-i);
tmpp=i;
}
ans=min(ans,ans1);
} cout<<ans<<endl;
return ;
}
D题:不是原创。
我们可以这样处理。
每次枚举第I个数。并且默认为根节点。且权值最大。然后我们的目的是找到多少个其子树是满足max-min<=d;
这里用DFS就好了。
#include<bits/stdc++.h>
#define N 12345
#define mod 1000000007
typedef long long ll;
using namespace std;
int a[N];
int con;
vector<int> mp[N];
int d; ll dfs(int p,int pre)
{
ll tot=;
for (int i=;i<mp[p].size();i++)
{
int v=mp[p][i];
if (pre==v||a[con]<a[v]||a[con]-a[v]>d||(a[con]==a[v]&&v<con)) continue;
tot=tot*(dfs(v,p)+)%mod;
}
return tot;
} int main()
{
int n;
cin>>d;
cin>>n;
for (int i=;i<=n;i++) cin>>a[i];
for (int i=;i<n;i++)
{
int x,y;
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
ll ans=;
for (int i=;i<=n;i++)
{
con=i;
ans=(ans+dfs(i,-))%mod;
}
cout<<ans;
}
E:继续补...不出来了。。。
还是忍不住看了题解。。
先说说自己的想法好了。。
解析:如果我们用二分做LIS 会发现我们做的事“字典序最小的LIS",怎么理解。。
比如 1 3 2 5.。我们会算出这样的3个数:1 2 5.
其实很多东西包含在这里。。
比如:我们预先算出每个数“对应多少个”。 比如 1 4 2 5 7 3 9
1 2 2 3 4 3 5
算出这样的数列
那么答案出来了 ai=4&&ai=2 是两个答案。 ai=5输出一种,ai=3不再LIS 里面。
然后代码就是这样了:
#include<bits/stdc++.h>
using namespace std;
#define N 123456
int a[N],b[N],c[N];
int good[N];
int dp[N];
int now[N]; int main()
{
int n;
cin>>n;
for (int i=;i<=n;i++) cin>>a[i]; int t=;
for (int i=;i<=n;i++)
{
if (a[i]>b[t]) {b[++t]=a[i];dp[i]=t;}
else
{
int x=lower_bound(b+,b+t+,a[i])-b;
b[x]=a[i];
dp[i]=x;
}
} int mx=t; for (int i=n;i>=;i--)
if (dp[i]==mx||now[dp[i]+]>a[i])
{
good[i]=dp[i];
c[good[i]]++;
now[dp[i]]=max(now[dp[i]],a[i]);
} for (int i=;i<=n;i++)
{
if (c[good[i]]==) cout<<;
else if (c[good[i]]==) cout<<;
else cout<<;
}
return ;
}
Codeforces Round #277 (Div. 2)的更多相关文章
- 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,输出任意一 ...
随机推荐
- 限制<input>输入内容 只允许数字 或者 字母
只能输入数字: 有回显 <input onkeyup="value=value.replace(/[^\d]/g,'')"> 只能输入数字:无回显 <input ...
- How to display SSRS report based on customer/Vendor specific language [AX2012]
Common requirement is to show the reports in customer’s language. [example : Quotations, sales confi ...
- sqlalchemy - day4
query 此文算是自己的一个总结,不敢说对sqlalchemy有多精通,只能算是入门的总结,免得后面忘记了这些个基本的东西.数据库的增,删,改,查,前面已经介绍了session的增,删,改,现在来介 ...
- Ztack学习笔记(6)-广播组播点播
Zigbee网络中进行数据通信主要有三种类型:单播.组播.广播.那这三种方式如何设置呢,在哪里设置呢, 一. 广播 当应用程序需要将数据包发送给网络的每一个设备时,使用这种模式.广播的短地址有三种 0 ...
- EmguCV学习——简单使用
关于EmguCV我就不多说了,是对应于OpenCV的一套net库. 公司是视觉方面的业务,我又不会c++(好想会啊,正在学习中).由于各种需求,自己觉得对c++不是特别感冒,所以选用了net下的ope ...
- Java 逻辑运算符、位运算符、移位操作符 总结(Java 学习中的小记录)
Java 逻辑运算符.位运算符.移位操作符 总结 作者:王可利(Star·星星) 逻辑运算符,表格如下: 解析逻辑运算符表: 1.与 (&) 特点:两个都为真的时候,结果为真.两个为 ...
- 删除redo所有日志,数据库无法启动
半夜在itpub上看到有人发贴,说不小心删除了redo所有日志,导致数据库无法启动,因此模拟了一下. 如下: OS: Oracle Linux Server release 5.7 DB: O ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- java intellij 写控制台程序 窗口程序
建一个空项目,建一个main函数 用application,就可以运行了 /** * Created by robin on 15/10/11. */public class hello { pu ...
- PHP中使用CURL实现GET和POST请求
转自:http://www.smsyun.com/home-index-page-id-284.html 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议, ...