NC24078. [USACO 2017 Feb B]Why Did the Cow Cross the Road
描述
As part of the study, Farmer John has been asked to document the number of times each of his cows crosses the road. He carefully logs data about his cows' locations, making a series of N observations over the course of a single day. Each observation records the ID number of a cow (an integer in the range 1…10, since Farmer John has 10 cows), as well as which side of the road the cow is on.
Based on the data recorded by Farmer John, please help him count the total number of confirmed crossings. A confirmed crossing occurs when a consecutive sightings of a cow place it on different sides of the road.
输入描述
The first line of input contains the number of observations, N, a positive integer at most 100. Each of the next N lines contains one observation, and consists of a cow ID number followed by its position indicated by either zero or one (zero for one side of the road, one for the other side).
输出描述
Please compute the total number of confirmed crossings.
示例1
输入:
8 3 1 3 0 6 0 2 1 4 1 3 0 4 0 3 1
输出:
3
说明:
In this example, cow 3 crosses twice -- she first appears on side 1, then later appears on side 0, and then later still appears back on side 1. Cow 4 definitely crosses once. Cows 2 and 6 do not appear to cross.C(clang11) 解法, 执行用时: 2ms, 内存消耗: 376K, 提交时间: 2020-11-28 12:38:37
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int a[105]; int main() { int n; scanf("%d", &n); memset(a, -1, sizeof(a)); int sum = 0; for (int i = 1;i <= n;i++) { int cnt; scanf("%d", &cnt); int aa = a[cnt]; scanf("%d", &a[cnt]); if (aa != -1 && aa != a[cnt]) sum++; } printf("%d", sum); }
Go(1.14.4) 解法, 执行用时: 3ms, 内存消耗: 1020K, 提交时间: 2020-11-30 10:40:09
package main import "fmt" var c[11] int func main(){ var n int fmt.Scanf("%d",&n) var s int s = 0 for i:=0;i < n; i ++{ var a,b int fmt.Scanf("%d %d",&a,&b) b = b + 1 if c[a] > 0 && c[a] != b{ s ++; } c[a] = b } fmt.Println(s) }
Python3(3.9) 解法, 执行用时: 21ms, 内存消耗: 2816K, 提交时间: 2020-11-28 13:35:58
N = int(input()) d = {} ans = 0 while N > 0: N -= 1 cow, pos = map(int, input().split()) if cow not in d: d[cow] = pos continue lastpos = d[cow] if pos != lastpos: ans += 1 d[cow] = pos print(ans)
C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 384K, 提交时间: 2020-11-30 13:02:32
#include<stdio.h> int main() { int a[12],n=12,x,y,S=0; while(n--) a[n]=-1; scanf("%d",&n); while(n--) { scanf("%d%d",&x,&y); if(a[x]==-1) a[x]=y; else if(a[x]!=y) a[x]=y,S++; } printf("%d\n",S); return 0; }