Codeforces Round #303 (Div. 2)
A.Toy Cars
题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车。
思路:简单题。遍历即可。
#include<iostream>
#include<cstdio>
using namespace std;
int mp[][];
int goodcar[];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++) scanf("%d", *(mp + i) + j);
}
int cnt = ;
for (int i = ; i <= n; i++)
{
bool ok = true;
for (int j = ; j <= n; j++)
{
if (j == i) continue;
if (mp[i][j] == || mp[i][j] == ||mp[j][i]==||mp[j][i]==)
{
ok = false;
break;
}
}
if (ok) goodcar[cnt++] = i;
}
printf("%d\n", cnt);
if (cnt)
{
for (int i = ; i < cnt; i++)
{
if (i) printf(" ");
printf("%d", goodcar[i]);
}
printf("\n");
}
return ;
}
B. Equidistant String
题意:给出两个相同长度的字符串,求是否存在一个字符串使得其与给出的两个字符串的在同一位置不同字符的个数相同。
思路:如果原本两个字符串之间不同的字符为偶数,则存在。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[];
char t[];
int main()
{
scanf("%s%s", s, t);
int len = strlen(s);
int diff = ;
for (int i = ; i < len; i++) if (s[i] != t[i]) diff++;
if (diff % ) printf("impossible\n");
else
{
int cnt = ;
for (int i = ; i < len; i++)
{
if (s[i] == t[i]) printf("%c", s[i]);
else
{
if (cnt < diff / ) printf("%c", s[i]);
else printf("%c", t[i]);
cnt++;
}
}
} return ;
}
C. Woodcutters
题意:一条路上有n棵树,给出其坐标和高度。现在有一个伐木工从1开始向右伐木,每遇见一棵树选择伐或者不伐,但必须遵守以下规则:假如其坐标为xi,高度为hi,那么如果伐木并让它向左倒,则其会占据[xi-hi,xi]的长度;如果向右倒,则会占据[xi,xi+hi]的长度,否则不砍的话只会占据xi这个位置。并且如果砍伐后其倒下所占据的地方不能被其他树占据。问最多能砍多少树?
思路:
1)贪心+模拟。
①第一棵直接向左倒。
②非第一棵树,记为cur,如果能够向前倒标记为true,且x则直接砍.
③当前cur不符合②时,如果向后倒同时下一棵树也能向前倒,且间距能容纳,则都砍,cur=cur+2,并设置向前倒标记为true,回到②,否则到④;
④如果cur能向后倒,cur+1能向前倒,但是间距只能容纳一棵树,我们需要枚举t=cur+2,判断t-1与t能否砍,直至cur==n或者t-1、t两棵树之间的间距至少比其中一棵树小或者t-1、t都能被间距容纳。如果为第一种情况,则cur~n-1的树都能砍;第二种情况,如果只比其中一棵树小,cur~t中只能砍t-cur棵,并且如果第t棵能够向前倒,则选择砍第t棵,而非t-1棵,重设向前倒标记,否则比最后两棵树都小,则只能砍t-cur-1棵;第三种情况,则都砍,cur=t+1,并设置向前倒标记为true,回到②;
⑤如果cur=n-1,则直接砍。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int px[];
int ph[];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++) scanf("%d%d", px + i, ph + i);
int cnt = ;
bool canfront = true;
for (int i = ; i < n; i++)
{
if (i == ||i==n-) cnt++;
else if (i < n - )
{
if (canfront)
{
if (px[i] - ph[i] > px[i - ])
{
cnt++;
continue;
}
}
if (px[i] + ph[i] < px[i + ])
{
if (px[i + ] - ph[i + ] > px[i])
{
if (px[i + ] - ph[i + ] > px[i] + ph[i]) cnt += , i++, canfront = true;
else
{
int t = i + ;
while (t < n&&px[t - ] + ph[t - ]<px[t] && px[t] - ph[t]>px[t - ] && px[t - ] + ph[t - ] >= px[t] - ph[t]) t++;
if (t == n) cnt += n - i, i = n, canfront = true;
else if (px[t - ] + ph[t - ] < px[t] - ph[t]) cnt += t - i + , i = t, canfront = true;
else if (px[t - ] + ph[t - ] < px[t])cnt += t - i, i = t-, canfront = false;
else if (px[t] - ph[t] > px[t - ]) cnt += t - i, i = t, canfront = true;
else cnt += t - i - , i = t - , canfront = false;
}
}
else cnt += ,canfront=false;
}
else canfront = true;
}
}
printf("%d\n", cnt); return ;
}
2)另一种贪心+模拟
①第一棵向左倒,最后一棵向右倒
②其余的树,如果能够向前倒,则+1;否则如果能向后倒,+1并且更新xi=xi+hi.
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int px[];
int ph[];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++) scanf("%d%d", px + i, ph + i);
int cnt = ;
bool canfront = true;
for (int i = ; i < n; i++)
{
if (i == ||i==n-) cnt++;
else if (i < n - )
{
if (px[i] - ph[i] > px[i - ]) cnt++;
else
{
if (px[i] + ph[i] < px[i + ]) cnt++, px[i] += ph[i];
}
}
}
printf("%d\n", cnt);
return ;
}
D. Queue
题意:有一队人在排队,如果轮到他时其等待时间(为其前面所有人的服务时间之和)不大于其需要的服务时间,则其会感到满意。现在你可以随意调动人的位置,求满意的人的最大数目。
思路:先从小到大排序,然后从第一个枚举,符合条件则+1,等待时间+ti,否则不管他。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t[];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%d", t + i);
}
sort(t, t + n);
long long tot = ;
int cnt = ;
for (int i = ; i < n; i++)
{
if (t[i] >= tot) cnt++, tot += t[i];
}
printf("%d\n", cnt); return ;
}
E. Paths and Trees
题意:有一个无向带权图,给出顶点,求一棵以该顶点为根的树,其每条边之和最小。
思路:SPFA变形,更新到该点的距离和连接该点的边。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = ;
const int maxm = ;
struct edge
{
int id, to,cost,next;
edge(int ii=,int tt=,int cc=,int nn=):id(ii),to(tt),cost(cc),next(nn){}
};
int Head[maxn], totedge;
edge Edge[maxm * ];
void addedge(int id,int from, int to, int w)
{
Edge[totedge] = edge(id, to, w, Head[from]);
Head[from] = totedge++;
Edge[totedge] = edge(id, from, w, Head[to]);
Head[to] = totedge++;
}
long long dis[maxn];
int pre[maxn];
bool vis[maxn];
const long long INF =0x3f3f3f3f3f3f3f3f;
void SPFA(int st)
{
queue<int>q;
q.push(st);
vis[st] = true;
memset(dis, INF, sizeof(dis));
memset(pre, -, sizeof(pre));
dis[st] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for (int i = Head[u]; i != -; i = Edge[i].next)
{
int v = Edge[i].to;
if (dis[v] > dis[u] + Edge[i].cost)
{
dis[v] = dis[u] + Edge[i].cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
else if (dis[v] == dis[u] + Edge[i].cost)
{
if (Edge[pre[v]].cost > Edge[i].cost)
{
pre[v]=i;
}
}
}
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
memset(Head, -, sizeof(Head));
totedge = ;
for (int i = ; i <= m; i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
addedge(i, u, v, c);
}
int start;
scanf("%d", &start);
SPFA(start);
long long tot = ;
for (int i=;i<=n; i++)
{
if (i == start) continue;
tot += 1ll * Edge[pre[i]].cost;
}
printf("%I64d\n", tot);
bool first = true;
for (int i=; i<=n; i++)
{
if (i == start) continue;
if (first) printf("%d", Edge[pre[i]].id), first = false;
else printf(" %d", Edge[pre[i]].id);
}
printf("\n");
return ;
}
Codeforces Round #303 (Div. 2)的更多相关文章
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #303 (Div. 2) B. Equidistant String 水题
B. Equidistant String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- Codeforces Round #303 (Div. 2) A. Toy Cars 水题
A. Toy Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...
- 「日常训练」Queue(Codeforces Round 303 Div.2 D)
简单到让人不敢相信是D题,但是还是疏忽了一点. 题意与分析 (Codeforces 545D) 题意:n人排队,当一个人排队的时间超过他需要服务的时间就会厌烦,现在要求一个最优排列使得厌烦的人最少. ...
- 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)
这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...
随机推荐
- Installing scipy on redhat with error “no lapack/blas resources found”
这是更新scipy出现的结果,需要新版本的scipy,而机器上只装了0.7的版本,更新的时候报错,找到了一个解决方法: wget http://mirror.centos.org/centos/6/o ...
- Java释出的时候,AWT作为Java最弱的组件受到不小的批评
Java释出的时候,AWT作为Java最弱的组件受到不小的批评. 最根本的缺点是AWT在原生的用户界面之上仅提供了一个非常薄的抽象层. 例如,生成一个AWT的 复选框会导致AWT直接调用下层原生例程来 ...
- linux memcache 安装
一,安装所要的软件 wget http://www.monkey.org/~provos/libevent-1.2.tar.gz #下载libevent 下面是下载memcache服务哭端memcac ...
- ASP.NET动态添加控件一例
第一次单击页面中有3个Label,第二次单击有6个,第三次单击有9个,也就是每次单击要在上次的状态下再添加3个. 我的方法是,可以通过Session来保存上次的状态,一种解法如下: Test.aspx ...
- numpy 和 pandas 中常用的一些函数及其参数
numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中. numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值. ...
- Linux系统中last命令的用法
1.作用 linux系统中last命令的作用是显示近期用户或终端的登录情况,它的使用权限是所有用户.通过last命令查看该程序的log,管理员可以获知谁曾经或企图连接系统. 2.格式 last [—R ...
- CocoaPods安装和使用教程Code4App LOGO
本文转载至 http://code4app.com/article/cocoapods-install-usage Code4App 原创文章.转载请注明出处:http://code4app.com/ ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
- MVC 发布程序 HTTP 错误 403.14 - Forbidden 及 HTTP 错误 404.2 - Not Found
新建立的MVC项目发布程序后会浏览网站可能会有问题 这时不要去按照系统提示打开“目录浏览”,而是应该去做一些配置 具体步骤: 1:配置web.Config <system.webServer&g ...
- 170214、mybatis一级和二级缓存
mybatis一级缓存是指在内存中开辟一块区域,用来保存用户对数据库的操作信息(sql)和数据库返回的数据,如果下一次用户再执行相同的请求, 那么直接从内存中读数数据而不是从数据库读取. 其中数据的生 ...