1、Automated Checking Machine
6821 Automated Checking Machine
The Internet Computer Parts Company (ICPC) is an on-line shop that sells computer parts. Pairs
of in-line electrical connectors are among the most popular parts that ICPC sells. However, they are
also one of the parts that are returned more often by unsatisfied customers, because due to errors in
packaging the connectors sent to the costumers may not be compatible.
An in-line connector is composed of five connection points, labelled from 1 to 5. Each connection
point of a connector can be either a plug or an outlet. We say two connectors are compatible if, for
every label, one connection point is a plug and the other connection point is an outlet (in other words,
two connectors are compatible if, for every connection point with the same label, a plug and an outlet
meet when the two connectors are connected).
The figure below shows examples of two connectors that are compatible and two connectors that
are not compatible.
ICPC is introducing a state-of-the-art Automated Checking Machine (ACM), with an optical
checker, which will verify whether the two connectors packaged for a customer are indeed compatible.
The complex and expensive hardware of the ACM is ready, but they need your help to finish the
software.
Given the descriptions of a pair of in-line connectors, your task is to determine if the connectors are
compatible.
Input
The input contains several test cases; each test case is formatted as follows. The first line contains five
integers Xi (0 ≤ Xi ≤ 1 for i = 1, 2, . . . , 5), representing the connection points of the first connector
in the pair. The second line contains five integers Yi (0 ≤ Yi ≤ 1 for i = 1, 2, . . . , 5), representing the
connection points of the second connector. In the input, a ‘0’ represents an outlet an a ‘1’ represents a
plug.
Output
For each test case in the input, output a line with a character representing whether the connectors are
compatible or not. If they are compatible write the uppercase letter ‘Y’; otherwise write the uppercase
letter ‘N’.
Sample Input
1 1 0 1 0
0 0 1 0 1
1 0 0 1 0
1 0 1 1 0
Sample Output
Y
N
题意:
给定字符串a,b,如果这两个字符串的每一位不同的话,说明是兼容的,输出Y,否则输出N
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define esp 1.0e-3
#define PI acos(-1)
int main()
{
int a[5],b[5];
int sum=0;
while(scanf("%d %d %d %d %d",&a[0],&a[1],&a[2],&a[3],&a[4])!=EOF)
{
sum=0;
scanf("%d %d %d %d %d",&b[0],&b[1],&b[2],&b[3],&b[4]);
for(int i=0;i<=4;i++)
if(a[i]==b[i])
sum++;
if(sum==0)
printf("Y\n");
else
printf("N\n");
}
}
2、Counting substhreengs (折磨死人的一道 dp 题)
6823 Counting substhreengs
Substrings are strings formed by choosing a subset of contiguous characters from a string. This is well
known. A little more obscure is the definition of substhreengs. A substhreeng is a substring which
complies to the following additional requirements:
It is non-empty, and composed entirely of base 10 digits.
Interpreted in base 10 (allowing extra leading zeros), the resulting integer is a multiple of 3.
For instance, the string “130a303” contains 9 substhreengs: the substhreeng ‘3’ three times, the
substhreengs ‘30’ and ‘0’ twice each, and the substhreengs ‘303’ and ‘03’ once each. The substring
‘30a3’ is not a substhreeng because it is not composed entirely of base 10 digits, while the substring
‘13’ is not a substhreeng because 13 is not a multiple of 3.
Notice that two substhreengs are considered different if they are different in length or start at a
different position, even if the selected characters are the same.
Given a string, you are asked to count the number of substhreengs it contains.
Input
The input contains several test cases; each test case is formatted as follows. A test case consists of a
single line that contains a non-empty string S of at most 106
characters. Each character of S is either
a digit or a lowercase letter.
Output
For each test case in the input, output a line with an integer representing the number of substhreengs
contained in S.
Sample Input
130a303
0000000000
icpc2014regional
Sample Output
9
55
2
题意:
给定一个字符串,在这个字符串中找子串,如果该子串(至少包含一个数字不能是字母,必须是连续的)的和是3的倍数,就满足题意,问满足题意的子串有几个?
思路:动态规划
dp[ i ]:代表字符串中每一位求余3余数是i(0||1||2)的个数
直接看代码吧~
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
//#define memset(a,n) memset(a,n,sizeof(n))
#define esp 1.0e-3
#define PI acos(-1)
const int MAXX=1e6+10;
LL dp[5];
char s[MAXX];
int main()
{
// 用data0,data1,data2的原因:如果直接将dp[2]赋值给dp[0] 的话,影响下边用到的dp[0],因为是用的是上一次的个数,而不是这一次的个数
LL ans=0,data,data0,data1,data2;
while(scanf("%s",s)!=EOF)
{
memset(dp,0,sizeof(dp));
ans=0;
LL len=strlen(s);
for(LL i=0;i<len;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
data=s[i]%3;
if(data==0)
{
data0=dp[0]+1; // dp[0]的个数等于上一次余数是0的个数+1 (1代笔本身这个数字)
data1=dp[1]; // dp[1] 的个数等于上一次余数是1的个数(此时余数是0,不影响余数1的个数)
data2=dp[2]; // dp[2]的个数等于上一次余数是2的个数(此时余数是0,不影响余数2的个数)
}
if(data==1)
{
data0=dp[2]; // dp[0] 的个数等于上一次余数是2的个数(此时余数是1,加上这个余数是1的数,
// 就会产生3的倍数,就会产生余数是0,使余数是0的个数增加
data1=dp[0]+1; // dp[1] 的个数等于上一次dp[0] 的个数(上一次余数是0的个数加上这一个余数是1的个
// 数就会产生余数是0的个数),加1代表这个数本身可以使余数是1的个数增加1
data2=dp[1]; // dp[2]的个数等于上一次dp[1]的个数(上一次余数是1的个数+这个余数是1的就会产生余数是2
}
if(data==2)
{
data0=dp[1];
data1=dp[2]; // dp[1] 的个数等于上一次dp[2]的个数+余数是2(2+2)%3会产生余数是1
data2=dp[0]+1;
}
dp[0]=data0;
dp[1]=data1;
dp[2]=data2;
ans+=dp[0]; // 结果是每次余数是0个结果之和
}
else
{
memset(dp,0,sizeof(dp));
}
}
printf("%lld\n",ans);
}
}
3、 Help cupid
Cupid’s job is getting harder, so he is adopting new technologies to help him with his difficult task of
matching people into happy couples. He appointed the best programmers in his staff to a new project
called Advanced Couples Matching (ACM). For this project, the programmers need to produce an
algorithm that takes a set of an even number of N lonely persons and matches them into N/2 couples,
such that each person is in exactly one couple.
Sadly, the data available about each person is limited. In this modern world, using gender, ethnicity,
age or nationality as criteria to form couples is not a sensible option, so the programmers can only use
data about the internet connection of each candidate. They decided to focus this stage on time zones.
People living in closer time zones are more likely to find time to interact with each other. Thus, the
programmers decided to create couples so as to minimize the total time difference.
Each time zone is identified by an integer between -11 and 12, inclusive, representing its difference
in hours from a particular time zone called Coordinated Universal Time (or UTC). The time difference
of two people living in time zones represented by integers i and j is the minimum between |i − j| and
24 − |i − j|. Given a partition of a set of an even number N of candidates into N/2 couples, its total
time difference is the sum of the time difference of each couple.
You are asked to write a program that receives as input the time zones of a set of N candidates.
The output of the program must be the minimum total time difference among all possible partitions of
the set into couples.
Input
The input contains several test cases; each test case is formatted as follows. The first line contains an
even integer N (2 ≤ N ≤ 1000) representing the number of candidates to be coupled. The second line
contains N integers T1, T2, …, TN (−11 ≤ Ti ≤ 12 for i = 1, 2, . . . , N) indicating the time zones of the
candidates.
Output
For each test case in the input, output a line with an integer representing the minimum total time
difference among all possible partitions of the set of candidates into couples.
Sample Input
6
-3 -10 -5 11 4 4
2
-6 6
8
0 0 0 0 0 0 0 0
Sample Output
5
12
0
题意:
给定n个数,保证n是偶数
把这n个数字分成n/2 对,每一对的值可以是 | x-y | 或者是 24- | x-y |
求这n/2对的最小和是多少
思路:
在这n/2对中每次找值小的,然后相加求和
关键是如何去找成对的值使得这两个的组合使结果最优?
首先按从小到大排序
例如 a b c d e f -10 -5 -3 4 4 11
我们可以有两种思路:
(1)、ab cd ef (挨着的组队, 每次求成对的最小的值,然后结果相加)
(-10,-5)(-3,4)(4,11) 产生的值分别是 5、7、7
(2)、af bc de (最小的和最大的组队可能会产生更小的值)
例如:-10 -5 -3 4 4 11
(-10,11)(-5,-3)(4,4) 产生的值分别是 3(24-(|-10-11|)=3)、2、0
最后的结果取这两种结果最小的一个
为什么只讨论这两种情况就可以呢?
一开始 -10 -5 -3 4 4 11 -10,-5 -3,4 4,11
把11 移至左边 11 -10 -5 -3 4 4 11,-10 -5,-3 4,4
把4 移至左边 4 11 -10 -5 -3 4 4,11 -10,-5 -3,4 (和第一种情况一样)
把4 移至左边 4 4 11 -10 -5 -3 4,4 11,-10 -5,-3 (和第二种情况一样)
可以知只有(1)、(2)这两种思路
(不得不说这个思路太强了趴~)
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define esp 1.0e-3
#define PI acos(-1)
int main()
{
int n;
int a[1010],b[1010];
int x,y,ans,ans1;
while(scanf("%d",&n)!=EOF)
{
ans=0;
ans1=0;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=0;i<n;i++)
{
if(i==0)
b[i]=a[n-1];
else
b[i]=a[i-1];
}
for(int i=0;i<n-1;i+=2)
{
x=abs(a[i]-a[i+1]);
ans+=min(x,24-x);
}
for(int i=0;i<n-1;i+=2)
{
y=abs(b[i]-b[i+1]);
ans1+=min(y,24-y);
}
printf("%d\n",min(ans,ans1));
}
}