南京理工大学第八届程序设计大赛(校外镜像)

(A) 偷吃糖果

Time Limit: 1000Ms

Memory Limit: 65536KB

Description
小鱼喜欢吃糖果。他有两盒糖果,两盒糖果分别仅由小写字母组成的字符串s和字符串t构成。其中’a’到’z’表示具体的某种糖果类别。
他原本打算送给他喜欢的女生,但是要送给女孩子的话两盒糖果不能有差别(即字符串s和t完全相同)。所以,他决定偷吃几块,他吃糖果的策略是每次选出一盒糖果中两个连续的同种类别的糖果,然后吃掉其中一块。该策略可以使用多次。
例如一盒糖果是’rrrjj’,他可以把这盒糖果变成’rrjj’或者’rrrj’。现在你要告诉小鱼,经过他多次偷吃糖果之后,两盒糖果能否送给他喜欢的女孩子。如果可以输出’Yes’,如果不行输出’No’。

Input
第一行一个T,表示T组测试数据。每组测试数据格式如下。
第一行表示字符串s,第二行表示字符串t。
1 ≤ T ≤ 100
Each character of s, t will be between ‘a’ and ‘z’.
1 ≤ length of string s ≤ 1000
1 ≤ length of string t ≤ 1000

Output
对于每组测试数据,输出Yes,或者No。

Sample Input
2
rrrjj
rrrj
rj
jr

Sample Output
Yes
No

Hint
题目中的第一个样例:第一盒糖果:rrrjj -> rrjj -> rjj -> rj第二盒糖果:rrrj -> rrj -> rjA

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 1000 + 10;
int t, n;
char str1[MAXN], str2[MAXN];

bool Is() {
  int len1 = strlen(str1);
  int len2 = strlen(str2);
  int i = 0, j = 1;
  while (j < len1) {
    if (str1[i] == str1[j]) {
      ++j;
    } else {
      str1[++i] = str1[j++];
    }
  }
  str1[i + 1] = 0;
  i = 0; j = 1;
  while (j < len2) {
    if (str2[i] == str2[j]) {
      ++j;
    } else {
      str2[++i] = str2[j++];
    }
  }
  str2[i + 1] = 0;
  return strcmp(str1, str2) == 0 ? true : false;
}

int main() {
  scanf("%d%*c", &t);
  while (t--) {
    scanf("%s%*c%s%*c", str1, str2);
    printf("%s\n", Is() ? "Yes" : "No");
  }
  return 0;
}

(C) count_prime

Time Limit: 1000ms

Memory Limit: 65536KB

Description
给定你一个数n,请你统计出在[a,b]这个区间中和n互质的数的个数。
两个数互质当且仅当他们除了1之外没有其他的公共因子或者他们最大的公共因子是1。1和任何数是互素的。

Input
第一行输入一个整数T(1 <= T <= 100),表示T组测试数据。
接下来T行,每行3个整数a,b,n(1 <= a <=b <=10^15, 1<= n <= 10^9),用空格隔开。

Output
输出一个整数表示和n互质的数的个数。

Sample Input
2
1 10 2
3 10 5

Sample Output
5
6

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;
int t;
LL a, b, n, prime[1000];

int GetPrime(LL num) {
  int cnt = 0;
  for (LL i = 2; i*i <= num; ++i) {
    if (num && num%i == 0) {
      prime[cnt++] = i;
      while (num && num%i == 0) num /= i;
    }
  }
  if (num > 1) prime[cnt++] = num;
  return cnt;
}

LL Multiple(int  cnt, LL arg) {
  LL ans = 0;
  for (LL i = 1; i < (LL)1 << cnt; ++i) {
    LL flag = 0, temp = 1;
    for (LL j = 0; j < cnt; ++j) {
      if (i & (LL)1<<j) { ++flag; temp *= prime[j]; }
    }
    if (flag & 1) ans += arg / temp;
    else ans -= arg / temp;
  }
  return ans;
}

int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld%lld", &a, &b, &n);
    int cnt = GetPrime(n);
    printf("%lld\n", b - Multiple(cnt, b) - (a - 1 - Multiple(cnt, a - 1)));
  }
  return 0;
}

(F) sequence

Time Limit: 1000MS

Memory Limit: 65536KB

Description
将一个给定的数列,拆分成K个不降序列,每个数出现且只出现一次,且在各序列中各个数相对于原数列的相对顺序不变。
如7 6 9 8 10可以拆成 7 9 10和6 8。求最小的K值。

Input
第一行输入一个整数T(1 <= T <= 100),表示接下来T组测试数据,
每组两行,第一行为n,代表数列长度(1<=n<=10000)
接下来一行有n个数,空格分隔(每个数<=50000)。

Output
对每组数据输出一个最小的K值。

Sample Input
2
5
7 6 9 8 10
5
5 4 3 2 1

Sample Output
2
5

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 10;
int t, n, arr[MAXN], lis[MAXN], dp[MAXN];

int Lis() {
  lis[1] = arr[1];
  int len = 1;
  for (int i = 1; i <= n; ++i) {
    int j = lower_bound(lis + 1, lis + 1 + len, arr[i]) - lis;
    if (len < j) { len = j; }
    lis[j] = arr[i];
  }
  return len;
}

int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int ans = 0;
    memset(dp, 0, sizeof(dp));
    memset(lis, 0, sizeof(lis));
    for (int i = n ; i > 0; --i) {
      scanf("%d", &arr[i]);
    }
    printf("%d\n", Lis());
  }
  return 0;
}

(H) 谁才是最强战舰!

Time Limit: 1000MS

Memory Limit: 65536KB

Description
依阿华来到镇守府的第一件事情,就是找大和solo!
然而这并不是什么好消息,说不定,镇守府,甚至佐伯湾就这么消失了。。。
于是,提督君想了一个简单的游戏,来分出她们的胜负。游戏规则如下:这里有N堆石子,每堆石子有a[i](1<=i<=N)个,每人轮流从其中的某一堆石子中拿出任意个石子(只能在其中一堆拿,不能不拿),大和先手,谁拿出了最后一个石子,谁输。若大和必胜,输出“Yamato_Saikou!”,若依阿华必胜,输出“Meidikeji_Shijiediyi!”,若两边都无法必胜,输出“Sayonara_Konosekai!”.

Input
第一行输入一个正整数T(1 <= T <= 1000),表示有T组测试数据。
对于每组测试数据:
第一行一个正整数,N(N<=1000),表示有N堆石子。
第二行N个整数a[i](1<=a[i]<=1000),表示每堆石子的数量。

Output
若大和必胜,输出“Yamato_Saikou!”,若依阿华必胜,输出“Meidikeji_Shijiediyi!”,若两边都无法必胜,输出“Sayonara_Konosekai!”.

Sample Input
3
1
5
2
1 2
3
1 1 1

Sample Output

Yamato_Saikou!
Yamato_Saikou!
Meidikeji_Shijiediyi!

#include <iostream>
using namespace std;

typedef long long LL;
int t, n;

int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int ans = 0, num1 = 0, num2 = 0, num;
    for (int i = 0; i < n; ++i) {
      scanf("%d", &num);
      num > 1 ? ++num1 : ++num2;
      ans ^= num;
    }
    if (n == 0) printf("Sayonara_Konosekai!");
    else if ((ans != 0 && num1 != 0) || (ans == 0 && num1 == 0)) {
      printf("Yamato_Saikou!\n");
    } else {
      printf("Meidikeji_Shijiediyi!\n");
    }
  }
  return 0;
}

(J) water1

Time Limit: 1000MS

Memory Limit: 65536KB

Description
听说全球气候变暖,冰川融化,海水淹大地。着实好奇要融化多少冰川会淹没我的宿舍,哦不,淹没南京,反正NJUST应该总会是第一批被淹的。
现将问题简化成一个二维坐标系的模型。有N个矩形块区域,每个区域有海拔(H)和宽度(W),区域按照顺序给出,比如有3个区域,第一个区域宽度为1,海拔为2,第二个区域宽度为5,海拔为6,第三个区域宽度为3,海拔为4,从图像上看就是像这样:(Y轴是海拔)

8
7
6 +++++
5 +++++
4 ++++++++
3 ++++++++
2 +++++++++
1 +++++++++
123456789

假设最左边和最右边都是无限高的墙。
为了简化问题,假设水流下来的速度是每秒一个单位的水,并且总是往区域一降水(某沿海地区)。
现在请问要淹没所有的区域至少要多少时间(秒)?淹没的定义是,所有的区域表面至少覆盖一层水。如果没有区域则至少要放一滴水。
上图例子中,淹没完如下:

8
7 wwwwwwwww
6 w+++++www
5 w+++++www
4 w++++++++
3 w++++++++
2 +++++++++
1 +++++++++
123456789

所以需要19秒。

Input
多CASE,测试数据以EOF结尾,对于每个CASE:
第一行一个整数N(0 <= N <= 10^5)。
接下去N行每行对应两个整数H(1 <= H <= 10^5),W(1 <= W <= 10^5)分别对应第N个区域的海拔和宽度。

Output
一个整数,表示所需要的时间。

Sample Input
3
2 1
6 5
4 3

Sample Output
19

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 1e5 + 10;
int n, h[MAXN], w[MAXN];

int main() {
  while (scanf("%d", &n) != EOF) {
    LL maxh = 0, sum = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%d%d", &h[i], &w[i]);
      if (maxh < (LL)h[i]) maxh = (LL)h[i];
    }
    maxh++;
    for (int i = 0; i < n; ++i) {
      sum += (maxh - h[i]) * (LL)w[i];
    }
    printf("%lld\n", sum == 0 ? 1 : sum);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值