NOIP2014 提高组模拟试题

第一试试题

题目概况:

中文题目名称

合理种植

排队

科技节

源程序文件名

plant.pas/.c/.cpp

lineup.pas/.c/.cpp

scifest.pas/.c/.cpp

输入文件名

plant.in

lineup.in

scifest.in

输出文件名

plant.out

lineup.out

scifest.out

每个测试点时限

1s

1s

1s

测试点数目

10

10

10

每个测试点分值

10

10

10

内存上限

128MB

128MB

128MB

1.      合理种植

(plant.pas/.c/.cpp)

【问题描述】

大COS在氯铯石料场干了半年,受尽了劳苦,终于决定辞职。他来到表弟小cos的寒树中学,找到方克顺校长,希望寻个活干。

于是他如愿以偿接到了一个任务……

美丽寒树中学种有许多寒树。方克顺希望校园无论从什么角度看都是满眼寒树,因此他不希望有三棵甚至更多寒树种在一条直线上。现在他把校园里n棵寒树的坐标都给了大COS,让他数出存在多少多树共线情况。(若一条直线上有三棵或以上的树,则算出现一个多树共线情况。)

【输入】

输入文件名为plant.in。

第1行一个正整数n,表示寒树棵数。

接下来n行,每行两个非负整数x、y,表示一颗寒树的坐标。没有两颗寒树在同一位置。

【输出】

输出文件名为plant.out。

输出一个整数,表示存在多少多树共线情况。

【输入输出样例】

plant.in

plant.out

6

0 0

1 1

2 2

3 3

0 1

1 0

1

【数据范围】

对于30%的数据,有n≤10;

对于50%的数据,有n≤100;

对于100%的数据,有n≤1,000,0≤x,y≤10,000。

2.      排队

(lineup.pas/.c/.cpp)

【问题描述】

小sin所在的班有n名同学,正准备排成一列纵队,但他们不想按身高从矮到高排,那样太单调,太没个性。他们希望恰好有k对同学是高的在前,矮的在后,其余都是矮的在前,高的在后。如当n=5,k=3时,假设5人从矮到高分别标为1、2、3、4、5,则(1,5,2,3,4)、(2,3,1,5,4)、(3,1,4,2,5)都是可行的排法。小sin想知道总共有多少种可行排法。

【输入】

输入文件名为lineup.in。

一行两个整数n和k,意义见问题描述。

【输出】

输出文件名为lineup.out。

输出一个整数,表示可行排法数。由于结果可能很大,请输出排法数mod 1799999的值。

【输入输出样例】

lineup.in

lineup.out

5 3

15

【数据范围】

对于20%的数据,有n≤10,k≤40;

对于60%的数据,有n≤100,k≤500;

对于100%的数据,有n≤100,k≤n*(n-1)/2。

3.      科技节

(scifest.pas/.c/.cpp)

【问题描述】

一年一度的科技节即将到来。同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那么多活动,还要不要认真学习了?!这样不行!……于是,校长要求减少一些活动,使每位学生只能参加一项(一名同学要参加某活动,必须已报名且该活动未被去掉)。当然,他也不希望哪位同学因此不能参加任何活动。他想知道自己的方案能否实行。

【输入】

输入文件名为scifest.in。

输入数据包括多组。

对于每组数据:

第一行两个正整数n和m,分别表示活动数和学生数。

接下来n行,每行m个为0或1的数。第i+1行第j列的数若为1,表示j同学报名参加活动i,否则表示j同学没有报名参加活动i。

【输出】

输出文件名为scifest.out。

对于每组数据输出一行,若校长方案可行则输出“Yes”,否则输出“No”。(均不包括引号)

【输入输出样例】

scifest.in

scifest.out

3 3

0 1 0

0 0 1

1 0 0

4 4

0 0 0 1

1 0 0 0

1 1 0 1

0 1 0 0

Yes

No

【数据范围】

对于20%的数据,n≤10,m≤200,数据组数≤10;

对于60%的数据,n≤16,m≤300,数据组数≤100;

对于100%的数据,n≤16,m≤300,数据组数≤1,000。

*************************************************Cut Line********************************************************

第一题

最初的O(2n2logn) 但是因为STL 的 map 卡常, T了一半

TLE代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn=;
const double eps=1e-;
inline int dcmp(double x)
{
if(fabs(x) <= eps) return ;
return x>0.0? : -;
}
struct Line
{
double k,b;
Line (const double _k,const double _b) { k=_k;b=_b; }
bool operator == (const Line l) const
{
return !dcmp(k - l.k) && !dcmp(b - l.b);
}
bool operator < (const Line l) const
{
if(dcmp(k - l.k)==) return b < l.b;
return k < l.k;
}
};
struct Point
{
int x,y;
}point[maxn];
#define x(i) point[i].x
#define y(i) point[i].y
map <Line,int> g;
map <Line,int> ::iterator it;
int non[maxn*];
int main()
{
freopen("plant.in","r",stdin);
freopen("plant.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
if(x(i) == x(j))
{
non[x(i)]++;
continue;
}
double k=(double) (y(i)-y(j)) / (x(i)-x(j));
double b=(double) y(i) - k * x(i);
if(!g.count(Line(k,b))) g[Line(k,b)]=;
else g[Line(k,b)]++;
}
int ans=;
for(it=g.begin();it!=g.end();it++)
{
if((*it).second>=) ans++;
}
for(int i=;i<=;i++)
if(non[i]>=) ans++;
printf("%d",ans);
return ;
}

某神犇想出来的方法, 枚举所有三元组, 用前面去改变后面的判断

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn=;
struct Point
{
int x,y;
}point[maxn];
#define x(i) point[i].x
#define y(i) point[i].y
int n;
typedef long long LL;
const LL base=(1ll<<); // must bigger than the 1e9 'coz the minimum of rake is 1/(9999*10000)
LL rake[maxn][maxn];
bool a[maxn][maxn];
int main()
{
freopen("plant.in","r",stdin) ;
freopen("plant.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&x(i),&y(i));
for(int i=;i^n;i++)
for(int j=i+;j<=n;j++)
rake[i][j] = x(i)==x(j)? 1ll : base * (y(i)-y(j)) / (x(i)-x(j));
int ans=;
for(int i=;i^(n-);i++) // not strictly n^3
for(int j=i+;j^n;j++)
for(int k=j+;k<=n;k++)
if(rake[i][j] == rake[j][k])
if(a[i][j] || a[j][k] || a[i][k]) a[i][j] = a[j][k] = a[i][k] = true;
else ans++, a[i][j] = a[j][k] = a[i][k] = true;
printf("%d",ans);
return ;
}

再给个标程, 每次给个标记, 同一个斜率如果x坐标在左边则说明之前算过, 借此可以排除掉斜率相同算过的情况

标程:

#include <fstream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream fin("plant.in");
ofstream fout("plant.out");
long n;
struct xxx{long x,y;} a[];
struct kk{long dx,dy;bool c;} k[];
bool operator <(kk k1,kk k2){
if (k1.dx==) return k2.dx<;
if (k2.dx==) return ;
return k2.dx*k1.dy<k1.dx*k2.dy;
}
bool operator ==(kk k1,kk k2){
if (k1.dx==||k2.dx==)
return k1.dx==k2.dx;
return k2.dx*k1.dy==k1.dx*k2.dy;
}
void init(){
long i;
fin>>n;
for (i=;i<n;i++)
fin>>a[i].x>>a[i].y;
}
void tr(){
long i,j,s,ans=;
bool f;
for (i=;i<n;i++){
for (j=;j<n;j++)
if (i!=j)
if (a[i].x==a[j].x){
k[j].dx=;
k[j].c=a[i].y<a[j].y;
}else{
k[j].dx=a[j].x-a[i].x;
k[j].dy=a[j].y-a[i].y;
if (k[j].dx<){
k[j].dx=-k[j].dx;
k[j].dy=-k[j].dy;
}
k[j].c=a[i].x<a[j].x;
}
for (j=i;j<n-;j++) k[j]=k[j+];
sort(k,k+n-);
f=; s=;
for (j=;j<n-;j++){
s++;
f=f&&k[j].c;
if (!(k[j]==k[j+])){
if (f&&s>) ans++;
f=; s=;
}
}
if (f&&k[n-].c&&s>) ans++;
}
fout<<ans;
}
int main(){
init();
tr();
fin.close();
fout.close();
return ;
}

第二题

动态规划但是可以用前缀和优化或者变换一下递推公式, 把相同部分替换掉

原来的 1AC 代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn=;
const int mod=;
int n,k;
int f[maxn][maxn*maxn];
int main()
{
freopen("lineup.in","r",stdin);
freopen("lineup.out","w",stdout);
scanf("%d%d",&n,&k);
memset(f,-,sizeof(f));
for(int i=;i<=n;i++) f[i][]=;
for(int i=;i<=n;i++)
for(int j=;j<=min(k,i*(i-)/);j++)
{
bool flag=true;
f[i][j]=;
for(int p=;p<=i-;p++)
{
if(!~f[i-][j-p]) continue;
flag=false;
f[i][j] += f[i-][j-p];
if(f[i][j]>=mod) f[i][j]%=mod;
}
if(flag) f[i][j]=-;
}
printf("%d",f[n][k]);
return ;
}

第三题

位运算 + 可行性剪枝(先考虑人数多的,容易更快发现冲突)

但是使用位向量生成法的话要一块一块地跳, 所以dfs更优

TLE代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn=;
const int maxm=;
int n,m;
struct Activity
{
int s[]; // max of 300 evry int has 32 bits
int cnt;
bool operator < (const Activity t) const
{
return cnt >= t.cnt;
}
}act[maxn];
inline bool init()
{
if(!~scanf("%d%d",&n,&m)) return false;
memset(act,,sizeof(act));
for(int i=;i<=n;i++)
{
for(int j=;j^m;j++)
{
int tmp;
scanf("%d",&tmp);
if(tmp) act[i].s[j>>] |= ( << (j - (j>><<)) ),act[i].cnt++;
}
}
sort(act+,act+n+);
return true;
}
int now[];
inline bool check()
{
for(int j=; j^(m->>); j++)
if(now[j]^(0xffffffff)) return false;
if(!(m->>)) return now[] ^ ((<<m)-)? false : true;
else return now[m->>] ^ ((<<m--(m->>))-)? false : true;
}
bool bit()
{
for(int status=; status<=(<<n)-; )
{
memset(now,,sizeof(now));
bool flag=false;
int pos=;
for(int i=;i<=n;i++)
{
if(!(status & (<<i-))) continue; // don't forget to check here!!
for(int j=;j<=(m->>);j++)
if((now[j] & act[i].s[j]) ^ ) { flag=true;pos=i-;break; }
else now[j] |= act[i].s[j];
if(flag) break;
}
if(flag) // step by blocks!!
{
status %= (<<pos);
status += (<<pos-);
}
else status++;
if(!check()) continue;
else return true;
}
return false;
}
int main()
{
freopen("scifest.in","r",stdin);
freopen("scifest.out","w",stdout);
while(init())
if(bit()) printf("Yes\n");
else printf("No\n");
return ;
}

标程:

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
FILE *fin=fopen("scifest.in","r"),*fout=fopen("scifest.out","w");
long n,m;
long x[][],h[];
bool yes;
void init(){
long i,j,k,c;
memset(x,,sizeof(x));
for (i=;i<n;i++){
h[i]=k=;
for (j=;j<m;j++){
fscanf(fin,"%ld",&c);
if (c==){
x[i][k/]+=<<(k%);
h[i]++;
}
k++;
}
}
for (i=;i<n-;i++)
for (j=i+;j<n;j++)
if (h[i]<h[j]){
k=h[i]; h[i]=h[j]; h[j]=k;
for (c=;c<;c++){
k=x[i][c]; x[i][c]=x[j][c]; x[j][c]=k;
}
}
}
void tr(long p,long sx[],long sh){
if (p==n){
if (sh==m) yes=;
return;
}
long i,t[];
for (i=;i<;i++){
if ((sx[i]&x[p][i])>) break;
t[i]=sx[i]|x[p][i];
}
if (i==) tr(p+,t,sh+h[p]);
if (!yes) tr(p+,sx,sh);
}
int main(){
fscanf(fin,"%ld%ld",&n,&m);
do{
init();
yes=;
tr(,x[],);
if (yes) fprintf(fout,"Yes\n");
else fprintf(fout,"No\n");
fscanf(fin,"%ld%ld",&n,&m);
}while (!feof(fin));
fclose(fin);
fclose(fout);
return ;
}

[位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15的更多相关文章

  1. HYSBZ(BZOJ) 4300 绝世好题(位运算,递推)

    HYSBZ(BZOJ) 4300 绝世好题(位运算,递推) Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<= ...

  2. Codeforces 631 (Div. 2) D. Dreamoon Likes Sequences 位运算^ 组合数 递推

    https://codeforces.com/contest/1330/problem/D 给出d,m, 找到一个a数组,满足以下要求: a数组的长度为n,n≥1; 1≤a1<a2<⋯&l ...

  3. Cayley-Hamilton定理与矩阵快速幂优化、常系数线性递推优化

    原文链接www.cnblogs.com/zhouzhendong/p/Cayley-Hamilton.html Cayley-Hamilton定理与矩阵快速幂优化.常系数线性递推优化 引入 在开始本文 ...

  4. P1466 集合 Subset Sums 搜索+递推+背包三种做法

    题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...

  5. [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式

    题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...

  6. 【CF607B】Zuma——区间dp(记忆化搜索/递推)

    以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...

  7. 【科技】位运算(bitset)优化最长公共子序列算法

    最长公共子序列(LCS)问题 你有两个字符串 \(A,B\),字符集为 \(\Sigma\),求 \(A, B\) 的最长公共子序列. 简单动态规划 首先有一个广为人知的 dp:\(f_{i,j}\) ...

  8. [CODEVS1295]N皇后(位运算+搜索)

    题目描述 Description 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个皇后,任 ...

  9. POJ 3220 位运算+搜索

    转载自:http://blog.csdn.net/lyhypacm/article/details/5813634 DES:相邻的两盏灯状态可以互换,给出初始状态.询问是否能在三步之内到达.如果能的话 ...

随机推荐

  1. java的socket 编程

    之前在学校的时候,有时间但是总是学不进去,现在工作了,总想多学点东西,但是又是没有时间来学习,只能在工作的闲暇之余,给自己充充电,随着学习的深入,越来月发现Java真的很强大,几乎什么都可以做的,以下 ...

  2. Notes(一)

    Numerous experimental measurements in spatially complex systems have revealed anomalous diffusion in ...

  3. eclipse内使用tomcat项目究竟被部署到了哪里

    笔者在使用eclipse+tomcat做本地调试,项目没跑起来,原因就很奇怪啊(某前辈说过:奇怪源于无知),然后就想它究竟是把项目放到哪个目录下呢,我的tomcat/webapps目录下并没有啊. 默 ...

  4. 使用hexdump 查看二进制文件

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  5. Icon specified in the Info.plist not found under the top level app wrapper: Icon.png

    For some reason the (possibly when adding multiple icons and changing the file?) the item gets moved ...

  6. 逗号分隔字符串转换为一张表--解决查询in(逗号分隔字符串)出错问题

    CREATE PROCEDURE [dbo].[Pro_TEST] AS BEGIN ) ) SET @split=',' SET @c='025,023,014,015' )) ) BEGIN IN ...

  7. UVa 131 - The Psychic Poker Player

    题目:手里有五张牌,桌上有一堆牌(五张).你能够弃掉手中的k张牌,然后从牌堆中取最上面的k个. 比較规则例如以下:(按优先级排序) 1.straight-flush:同花顺.牌面为T(10) - A, ...

  8. C#_mvc_ajax_return data

    假设cshtml文件中是这样的: <script type="text/javascript"> $(document).ready(function(){ $(&qu ...

  9. 【转】IT职场人生系列之四:怎样写简历

    本文是IT职场人生系列的第四篇. 因为早年跳槽无数,所以积累了不少"技巧",逐渐变成写简历的"专家",最长的时候简历到了12页,所以现在练就一手写长篇博客的功夫 ...

  10. next nextval

    1 KMP算法中next与nextval值的计算 以上两张图代表了next值的求法,本人总结后做如下叙述: 根据公式可知: next[1]=0 next[2]=1 next[3]的求法根据公式可以直接 ...