【BestCoder】【Round#42】

模拟+链表+DP



  Orz AK爷faebdc

A

  Growin要跟全部的n个人握手共2n杯香槟,再加上每对关系的两杯香槟,直接统计邻接矩阵中1的个数,再加2n就是answer

 1 //BestCoder 42 A
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define rep(i,n) for(int i=0;i<n;++i)
 9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 #define pb push_back
12 using namespace std;
13 inline int getint(){
14     int v=0,sign=1; char ch=getchar();
15     while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
16     while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
17     return v*sign;
18 }
19 const int N=1e5+10,INF=~0u>>2;
20 typedef long long LL;
21 /******************tamplate*********************/
22 int n,a[100][100];
23 int main(){
24 #ifndef ONLINE_JUDGE
25     freopen("A.in","r",stdin);
26     freopen("A.out","w",stdout);
27 #endif
28     while(scanf("%d",&n)!=EOF){
29         memset(a,0,sizeof a);
30         LL cnt=0;
31         F(i,1,n) F(j,1,n){
32             a[i][j]=getint();
33             if (a[i][j]) cnt++;
34         }
35         printf("%lld\n",cnt+2*n);
36     }
37     return 0;
38 }

B

  我一开始写了个以height为第一关键字,下标为第二关键字的set……每次直接lower_bound……然而TLE了

  后来改了改离散化+链表模拟……结果FST了

  查了半天想不出算法哪里错了……然后把10W的数组改成100W……过了

  以后这种空间复杂度为线性的题目……在允许的情况下还是尽量开大点好了……

 1 //BestCoder 42 B
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define rep(i,n) for(int i=0;i<n;++i)
 9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 using namespace std;
12
13 int getint(){
14     int v=0,sign=1; char ch=getchar();
15     while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();}
16     while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();}
17     return v*sign;
18 }
19 typedef long long LL;
20 const int N=1000100,INF=~0u>>2;
21 /*******************template********************/
22 int n,m,h[N],q[N],c[N<<1],head[N],v[N],nxt[N],cnt;
23 void add(int x,int y){
24     v[++cnt]=y; nxt[cnt]=head[x]; head[x]=cnt;
25 }
26 int main(){
27 #ifndef ONLINE_JUDGE
28     freopen("B.in","r",stdin);
29 //    freopen("output.txt","w",stdout);
30 #endif
31     while(scanf("%d%d",&n,&m)!=EOF){
32         F(i,1,n) c[i]=h[i]=getint();
33         F(i,1,m) c[n+i]=q[i]=getint();
34         memset(head,0,sizeof head); cnt=0;
35         sort(c+1,c+n+m+1);
36         int num=unique(c+1,c+n+m+1)-c-1;
37         D(i,n,1){
38             h[i]=lower_bound(c+1,c+num+1,h[i])-c;
39             add(h[i],i);
40         }
41         F(i,1,m){
42             q[i]=lower_bound(c+1,c+num+1,q[i])-c;
43             if (head[q[i]]==0) puts("-1");
44             else{
45                 printf("%d\n",v[head[q[i]]]);
46                 head[q[i]]=nxt[head[q[i]]];
47             }
48         }
49     }
50     return 0;
51 }

C

  直接背包DP吧……本来还想:会不会有人直接把ans的初值设为w[1][1]呢?然而w[1][1]>k?是不是可以hack一下……然而看了样例我发现我想多了……这场比赛的hack果然很少……

 1 //BestCoder 42 C
 2 #include<queue>
 3 #include<set>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<cstdlib>
 8 #include<iostream>
 9 #include<algorithm>
10 #define rep(i,n) for(int i=0;i<n;++i)
11 #define F(i,j,n) for(int i=j;i<=n;++i)
12 #define D(i,j,n) for(int i=j;i>=n;--i)
13 #define pb push_back
14 using namespace std;
15 inline int getint(){
16     int v=0,sign=1; char ch=getchar();
17     while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
18     while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
19     return v*sign;
20 }
21 const int N=110,INF=~0u>>2;
22 typedef long long LL;
23 /******************tamplate*********************/
24 int n,m,K,w[N][N];
25 bool f[N][N][N];
26 struct node{
27     int x,y,v;
28 };
29 queue<node>Q;
30 int main(){
31 #ifndef ONLINE_JUDGE
32     freopen("C.in","r",stdin);
33     freopen("C.out","w",stdout);
34 #endif
35     while(scanf("%d%d%d",&n,&m,&K)!=EOF){
36         memset(w,0,sizeof w);
37         memset(f,0,sizeof f);
38         F(i,1,n) F(j,1,m) w[i][j]=getint();
39         Q.push((node){1,1,0});
40         int ans=0;
41         if (w[1][1]<=K){
42             ans=w[1][1];
43             Q.push((node){1,1,w[1][1]});
44         }
45         while(!Q.empty()){
46             int x=Q.front().x,y=Q.front().y,v=Q.front().v; Q.pop();
47             if (x<n){
48                 if (!f[x+1][y][v]){
49                     f[x+1][y][v]=1;
50                     Q.push((node){x+1,y,v});
51                 }
52                 if (v+w[x+1][y]<=K && !f[x+1][y][v+w[x+1][y]]){
53                     f[x+1][y][v+w[x+1][y]]=1;
54                     Q.push((node){x+1,y,v+w[x+1][y]});
55                     ans=max(ans,v+w[x+1][y]);
56                 }
57             }
58             if (y<m){
59                 if (!f[x][y+1][v]){
60                     f[x][y+1][v]=1;
61                     Q.push((node){x,y+1,v});
62                 }
63                 if (v+w[x][y+1]<=K && !f[x][y+1][v+w[x][y+1]]){
64                     f[x][y+1][v+w[x][y+1]]=1;
65                     Q.push((node){x,y+1,v+w[x][y+1]});
66                     ans=max(ans,v+w[x][y+1]);
67                 }
68             }
69         }
70         printf("%d\n",ans);
71     }
72     return 0;
73 }

时间: 2024-08-25 05:25:37

【BestCoder】【Round#42】的相关文章

【codeforces VK Cup Round 1】BDE题解

B. Group Photo 2 (online mirror version) time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Many years have passed, and n friends met at a party again. Technologies have leaped forward since the

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter&#39;s Song

D. DZY Loves FFT Source http://codeforces.com/contest/445/problem/D Description Wherever the destination is, whoever we meet, let's render this song together. On a Cartesian coordinate plane lies a rectangular stage of size w?×?h, represented by a re

【BestCoder】#29 C GTY&#39;s gay friends(区间和 随机数判重)

题目大意:可以到相应的场次查看中文翻译. 思路:其实这道题很简单,对于一个等差数列,我们要判断他是否每个数都出现,只需要判断区间和或者是最大值是否符合即可,但这边需要注意的便是中间的重复部分.最大值的判重必要性我就不知道了,而且我也不会做,目测做也超时. 这边就写一下偷别人的区间和+随机数判重的做法 其实这边判重的方法是给一个数加上一个超过1000007的权,然后在计算和的时候,便是唯一的. 否则例如下面的情况 10 11的和可以由5和16构成,既然两个的和可以被另外一个的两个数替代,那我们就找

【POJ 1584】 A Round Peg in a Ground Hole (判凸包+判圆在凸包内)

[POJ 1584] A Round Peg in a Ground Hole (判凸包+判圆在凸包内) 这题题面是一大坑..长长的 明显是给我这种英语渣准备的... 大体意思是给出一个多边形的点 按顺时针或逆时针给出 判断是否为凸包 同时给出一个圆(圆心坐标+半径) 问这个圆在不在多边形内 首先顺逆时针不确定 我的做法是输入时先判断顺时针还是逆时针输入 然后统统变成逆时针来走 就是根据两种情况传入不同的枚举起点 终点 和加减(具体见代码 判凸包用建凸包的叉成法即可 既然逆时针走 那么如果是凸包

hdu 4908 BestCoder Sequence【DP】

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目大意:给出一个排列,一个m,求出这个排列的连续子序列中有多少个序列式以m为中位数. 由于是一个排列,不会出现重复的数字,记录一下m的位置index,然后以index为分界线,往左求出s[i](表示从i到index之间有多少大于m),b[i](表示从i到index之间有多少小于m),往右求出s[i](表示从index到i之间有多少大于m),b[i](表示从index到i之间有多少小于m).

【推导】【分类讨论】Codeforces Round #431 (Div. 1) B. Rooter&#39;s Song

给你一个这样的图,那些点是舞者,他们每个人会在原地待ti时间之后,以每秒1m的速度向前移动,到边界以后停止.只不过有时候会碰撞,碰撞之后的转向是这样哒: 让你输出每个人的停止位置坐标. ①将x轴上初始坐标记为(pi,0),y轴上的初始坐标记为(0,pi).只有pi-ti相同的才有可能发生碰撞.于是可以按照这一点将人划分为很多组,不同组之间绝对不会互相影响. ②假设一组内每个人都不会发生碰撞,那么所有的路线交叉点都是碰撞点.所以碰撞次数可能达到n^2次,暴力不可行. ③对于一组内,形成了一个网格图

LeetCode:接雨水【42】

LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水). 感谢 Marcos 贡献此图. 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 题目分析 找出最高点 分别从两边往最高点遍历:如果下一个数比当前数小,说明可以接到水 Java题解 c

【手抖康复训练1 】Codeforces Global Round 6

[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思路手抖过不了样例,C题秒出思路手抖过不了样例*3 D题 手抖 过的了样例 ,调了1h,赛后发现变量名写错了,改一个字符就能AC... 题目等补完题一起放上来QAQ 原文地址:https://www.cnblogs.com/ttttttttrx/p/12110199.html

【python之路42】web框架们的具体用法

Python的WEB框架 (一).Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip install bottle easy_install bottle apt-get install python-bottle wget http://bottlepy.org/bottle.py Bottle框架大致可以分为以下部分: 路由系统,将不同请求交由指定函数处理 模板系统,将模板