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)的更多相关文章

  1. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  2. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  3. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  4. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  5. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  6. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  7. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

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

  9. 「日常训练」Queue(Codeforces Round 303 Div.2 D)

    简单到让人不敢相信是D题,但是还是疏忽了一点. 题意与分析 (Codeforces 545D) 题意:n人排队,当一个人排队的时间超过他需要服务的时间就会厌烦,现在要求一个最优排列使得厌烦的人最少. ...

  10. 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

    这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...

随机推荐

  1. Linux压缩解压缩命令学习笔记

    Linux中主要的压缩文件有:*.gz   *.tar  *.tar.gz  *.zip  *.bz2  *.tar.bz2 .zip这种古老的压缩格式,在window和Linux中都不需要安装软件可 ...

  2. Socket创建失败:10093错误

    10093的错误,应用程序没有调用 WSAStartup,或者 WSAStartup 失败. 问题描述:Failed to create UDP socket:10093!Close and rest ...

  3. mysq for visual studio 1.1.1

    https://cdn.mysql.com/Downloads/MySQLInstaller/mysql-visualstudio-plugin-1.1.1.msi

  4. (转)java并发对象锁、类锁、私有锁

    转自:http://ifeve.com/java-locks/ 建议参考:http://www.zhihu.com/question/28113814 Java类锁和对象锁实践 感谢[jiehao]同 ...

  5. MathType可以编辑带圈乘号吗

    在数学中有很多符号,可能这些符号我们用得上,也有些符号我们很少用,甚至用不上,但是我们用不上,不代表不存在这个符号,也不代表别人用不上,只是各自所涉及到的知识领域不一样而已.而对于加减乘除运算,几乎每 ...

  6. HTTP请求的过程&HTTP/1.0和HTTP/1.1的区别&HTTP怎么处理长连接

    http://www.cnblogs.com/GumpYan/p/5821193.html

  7. Import error: no module named cv2 错误解决方法

    Windows: 将opencv安装目录下的cv2.pyd拷贝到Python安装目录里Lib中site-packages Linux: (1)将opencv安装目录下的cv2.so拷贝到Python安 ...

  8. Poj3087

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8842   Accepted: 4077 Desc ...

  9. Poj1426

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25721   Accepted: 106 ...

  10. iOS 7.1 UITapGestureRecognizer 不好用的解决办法

    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(o ...