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,输出任意一 ...
随机推荐
- Android工程目录及其作用简介
1. src:存放所有的*.java源程序. 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID. 3. assets:可以存放项目一些较大的资源文件,例如: ...
- 十天学会单片机Day4串行口通信
并行与串行基本通信方式 1.并行通信方式 通常是将数据字节的各位用多条数据线同时进行传送. 并行通信控制简单.传输速度快:由于传输线较多,长距离传送时成本高且接收方的各位同时接收存在困难. 2.串行通 ...
- 第四章 管理程序流(In .net4.5) 之 事件和回调
1. 概述 本章讲解如何使用 委托.lambda表达式 和 匿名方法 来创建和使用事件. 2. 主要内容 2.1 理解委托 委托是一种用方法签名形式定义的类型.可以让它指向其他方法,可以通过它调用其他 ...
- C#巧用Excel模版变成把Table打印出来
将一个做好的Excel模版,通过程序填上数据然后打印出来这个需求有两种方法一种是通过代码打开Excel模版然后填入数据然后再打印. 第二种方法就是我将要介绍的 1.将Excel设置好格式另存为HTML ...
- hdu 1867 A + B for you again
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...
- swift学习初步(四)-- 函数
好了,让我们开始接着前几天写的系列博客开始今天的这篇博客.在swift里面如果你需要定义一个方法的话,你需要使用关键字:func,请看下面的这段代码: func sayHello(name:Strin ...
- Android里面的命名规范
前前后后接触安卓也有一段时间了,但是对于Android命名规范这块一直没有太注意过.导致有的时候写出来的代码,前后的风格根本不一样,今天在网上查了一下,正好对自己来说可以好好的总结一下. 首先在And ...
- 详解iOS多线程 (转载)
iPhone 中的线程应用并不是无节制的,官方给出的资料显示iPhone OS下的主线程的堆栈大小是1M,第二个线程开始都是512KB.并且该值不能通过编译器开关或线程API函数来更改. 只有主线程有 ...
- [工具]IL Mapper2(C# -> IL 转换器)
下载地址:IL_Mapper2_exe.zip 源文件:IL_Mapper2_src.zip 简介 此工具可以直接把C#代码转换成IL代码查看,省去编译和手动操作ildsam的繁琐.希望能对想研究IL ...
- [DllImport("kernel32.dll")]是什么意思??
转载自:http://blog.csdn.net/sp6645597/article/details/8683737 1.简单说明 这叫引入kernel32.dll这个动态连接库(顾名思义就是一个链接 ...