整理上次写的题目:

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.

Input

The single line contains the positive integer n (1 ≤ n ≤ 10^15).

题目简洁。可以看出规律。。。分下奇偶就可以了。

B:

B. OR in Matrix
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output

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

 

同样很繁琐的题目,但是我们可以贪心之。

贪心策略: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)的更多相关文章

  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. java android 中的Toast

    package com.example.my1; import android.os.Bundle;import android.app.Activity;import android.content ...

  2. 在CentOS6.5下安装Memcached

    CentOS 6.5 安装软件非常方便, yum install memcached

  3. Python核心编程--学习笔记--9--文件和输入输出

    本章将深入介绍Python的文件处理和相关输入输出能力,包括:文件对象(以及它的内建函数.内建方法和属性),标准文件,文件系统的访问方法,文件执行,最后简要涉及持久存储和标准库中与文件有关的模块. 1 ...

  4. webpack 学习笔记 01 使用webpack的原因

    本系列文章实际上就是官网文档的翻译加上自己实践过程中的理解. 伴随着websites演化至web apps的过程,有三个现象是很明显的: 页面中有越来越多的Js. 客户端能做的事情越来越多. 越来越少 ...

  5. 公钥私钥 ssl/tsl的概念

    一,公钥私钥1,公钥和私钥成对出现2,公开的密钥叫公钥,只有自己知道的叫私钥3,用公钥加密的数据只有对应的私钥可以解密4,用私钥加密的数据只有对应的公钥可以解密5,如果可以用公钥解密,则必然是对应的私 ...

  6. kettle插入/更新

    1.数据库环境 --------------------实时表 ),Info )); ,'张启山','长沙'); ,'尹新月','长沙'); ,'二月红','长沙'); --------------- ...

  7. Android--将字节数转化为B,KB,MB,GB的方法

    //将字节数转化为MB private String byteToMB(long size){ long kb = 1024; long mb = kb*1024; long gb = mb*1024 ...

  8. hdu 4417 Super Mario/树套树

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...

  9. windows phone 豆瓣api的封装

    利用周末的时候,重新封装一下豆瓣的api,就当是练手吧!其实现在网上好用的api很多,在这个demo里面基本上已经将整体框架搭建起来,本来想继续完善下去的.但是其实accesstoken的时候,一直拿 ...

  10. 数组链表下标指针map list

    1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...