Codeforces Round#630 div2 A~C题解
Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat!
Initially, Alice's cat is located in a cell (x,y)(x,y) of an infinite grid. According to Alice's theory, cat needs to move:
- exactly aa steps left: from (u,v)(u,v) to (u−1,v)(u−1,v);
- exactly bb steps right: from (u,v)(u,v) to (u+1,v)(u+1,v);
- exactly cc steps down: from (u,v)(u,v) to (u,v−1)(u,v−1);
- exactly dd steps up: from (u,v)(u,v) to (u,v+1)(u,v+1).
Note that the moves can be performed in an arbitrary order. For example, if the cat has to move 11 step left, 33 steps right and 22 steps down, then the walk right, down, left, right, right, down is valid.
Alice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area [x1,x2]×[y1,y2][x1,x2]×[y1,y2], i.e. for every cat's position (u,v)(u,v) of a walk x1≤u≤x2x1≤u≤x2 and y1≤v≤y2y1≤v≤y2 holds.
Also, note that the cat can visit the same cell multiple times.
Can you help Alice find out if there exists a walk satisfying her wishes?
Formally, the walk should contain exactly a+b+c+da+b+c+d unit moves (aa to the left, bb to the right, cc to the down, dd to the up). Alice can do the moves in any order. Her current position (u,v)(u,v) should always satisfy the constraints: x1≤u≤x2x1≤u≤x2, y1≤v≤y2y1≤v≤y2. The staring point is (x,y)(x,y).
You are required to answer tt test cases independently.
The first line contains a single integer tt (1≤t≤1031≤t≤103) — the number of testcases.
The first line of each test case contains four integers aa, bb, cc, dd (0≤a,b,c,d≤1080≤a,b,c,d≤108, a+b+c+d≥1a+b+c+d≥1).
The second line of the test case contains six integers xx, yy, x1x1, y1y1, x2x2, y2y2 (−108≤x1≤x≤x2≤108−108≤x1≤x≤x2≤108, −108≤y1≤y≤y2≤108−108≤y1≤y≤y2≤108).
For each test case, output "YES" in a separate line, if there exists a walk satisfying her wishes. Otherwise, output "NO" in a separate line.
You can print each letter in any case (upper or lower).
6
3 2 2 2
0 0 -2 -2 2 2
3 1 4 1
0 0 -1 -1 1 1
1 1 1 1
1 1 1 1 1 1
0 0 0 1
0 0 0 0 0 1
5 1 1 1
0 0 -100 -100 0 100
1 1 5 1
0 0 -100 -100 100 0
Yes
No
No
Yes
Yes
Yes
题意:给你一个初始点(x,y),要求x1<=x<=x2,y1<=y<=y2,求这个点能否在这个活动范围内向左右上下移动a,b,c,d次
题解:因为左右移动和上下移动是独立的,所以我们将左右移动和上下移动分开来看,加入这个活动的范围不是空集,那么我们总可以向左(上)走一步然后向右(下)走一步,然后判断多出来的步数是否在区间范围内即可
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <map>
10 #include <set>
11 #include <unordered_set>
12 #include <unordered_map>
13 #define ll long long
14 #define fi first
15 #define se second
16 #define pb push_back
17 #define me memset
18 const int N = 1e6 + 10;
19 const int M = 1e9 + 7;
20 using namespace std;
21 typedef pair<int,int> PII;
22 typedef pair<long,long> PLL;
23
24 int t;
25 ll a,b,c,d;
26 ll x,y,x1,x2,y2;
27 ll Y1;
28 int main() {
29 ios::sync_with_stdio(false);
30 cin>>t;
31 while(t--) {
32 cin >> a >> b >> c >> d;
33 cin >> x >> y >> x1 >> Y1 >> x2 >> y2;
34 if (x1 == x2 && (a || b)) {
35 printf("No\n");
36 continue;
37 }
38 if (Y1 == y2 && (c || d)) {
39 printf("No\n");
40 continue;
41 }
42 x+=b-a;
43 y+=d-c;
44 if(x>=x1 && x<=x2 && y>=Y1 && y<=y2) printf("Yes\n");
45 else printf("No\n");
46 }
47 return 0;
48 }
A positive integer is called composite if it can be represented as a product of two positive integers, both greater than 11. For example, the following numbers are composite: 66, 44, 120120, 2727. The following numbers aren't: 11, 22, 33, 1717, 9797.
Alice is given a sequence of nn composite numbers a1,a2,…,ana1,a2,…,an.
She wants to choose an integer m≤11m≤11 and color each element one of mm colors from 11 to mm so that:
- for each color from 11 to mm there is at least one element of this color;
- each element is colored and colored exactly one color;
- the greatest common divisor of any two elements that are colored the same color is greater than 11, i.e. gcd(ai,aj)>1gcd(ai,aj)>1 for each pair i,ji,j if these elements are colored the same color.
Note that equal elements can be colored different colors — you just have to choose one of mm colors for each of the indices from 11 to nn.
Alice showed already that if all ai≤1000ai≤1000 then she can always solve the task by choosing some m≤11m≤11.
Help Alice to find the required coloring. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some mm from 11 to 1111.
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then the descriptions of the test cases follow.
The first line of the test case contains a single integer nn (1≤n≤10001≤n≤1000) — the amount of numbers in a sequence aa.
The second line of the test case contains nn composite integers a1,a2,…,ana1,a2,…,an (4≤ai≤10004≤ai≤1000).
It is guaranteed that the sum of nn over all test cases doesn't exceed 104104.
For each test case print 22 lines. The first line should contain a single integer mm (1≤m≤111≤m≤11) — the number of used colors. Consider colors to be numbered from 11 to mm. The second line should contain any coloring that satisfies the above conditions. Print nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤m1≤ci≤m), where cici is the color of the ii-th element. If there are multiple solutions then you can print any of them. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some mm from 11 to 1111.
Remember that each color from 11 to mm should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than 11).
3
3
6 10 15
2
4 9
23
437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961
1
1 1 1
2
2 1
11
4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6
题意:给你n个合数,要求用m<=11种颜色来给他们图上颜色,每个数只能涂一种颜色,如果任意两个数的gcd>1,那么他们可以涂同一种颜色,相同的数也可以涂同一种颜色,要求输出颜色总数和种类
题解:该题的数据范围非常的小,因为每个数在1000以内,所以他们的质因数只有可能是(2,3,5,7,11,13,17,19,23,29,31),然后我们找每个数的最小质因数,如果他们的最小质因数相同,那么颜色也就相同,反之颜色种类++;
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <map>
10 #include <set>
11 #include <unordered_set>
12 #include <unordered_map>
13 #define ll long long
14 #define fi first
15 #define se second
16 #define pb push_back
17 #define me memset
18 const int N = 1e6 + 10;
19 const int M = 1e9 + 7;
20 using namespace std;
21 typedef pair<int,int> PII;
22 typedef pair<long,long> PLL;
23 int t;
24 int n,a[N],c[N];
25 int p[11]={2,3,5,7,11,13,17,19,23,29,31};
26 map<int,int> ans;
27
28 int main() {
29 ios::sync_with_stdio(false);
30 cin>>t;
31 while(t--){
32 me(c,0,sizeof(c));
33 ans.clear();
34 cin>>n;
35 for(int i=1;i<=n;++i) cin>>a[i];
36 int cnt=0;
37 for(int i=1;i<=n;++i)
38 for(int j=0;j<11;++j){
39 if(a[i]%p[j]==0){
40 if(c[j]==0) c[j]=++cnt;
41 ans[i]=c[j];
42 break;
43 }
44 }
45 map<int,int>::iterator iter;
46 printf("%d\n",cnt);
47 for(iter=ans.begin();iter!=ans.end();++iter) printf("%d ",iter->se);
48 printf("\n");
49 }
50 return 0;
51 }
Word ss of length nn is called kk-complete if
- ss is a palindrome, i.e. si=sn+1−isi=sn+1−i for all 1≤i≤n1≤i≤n;
- ss has a period of kk, i.e. si=sk+isi=sk+i for all 1≤i≤n−k1≤i≤n−k.
For example, "abaaba" is a 33-complete word, while "abccba" is not.
Bob is given a word ss of length nn consisting of only lowercase Latin letters and an integer kk, such that nn is divisible by kk. He wants to convert ss to any kk-complete word.
To do this Bob can choose some ii (1≤i≤n1≤i≤n) and replace the letter at position ii with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert ss to any kk-complete word.
Note that Bob can do zero changes if the word ss is already kk-complete.
You are required to answer tt test cases independently.
The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases.
The first line of each test case contains two integers nn and kk (1≤k<n≤2⋅1051≤k<n≤2⋅105, nn is divisible by kk).
The second line of each test case contains a word ss of length nn.
It is guaranteed that word ss only contains lowercase Latin letters. And it is guaranteed that the sum of nn over all test cases will not exceed 2⋅1052⋅105.
For each test case, output one integer, representing the minimum number of characters he has to replace to convert ss to any kk-complete word.
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
2
0
23
16
题意:给你一个长度为n的字符串s,要求对于1<=i+k<=n,有s[i]=s[i+k],且s是一个回文串,问要达到这样的要求,至少要修改多少个字母(n可以被k整除)
题解:因为s必须是回文串,所以,要把s改成n/k个相等的串,并且每个串必须是回文串,我们可以直接遍历得出长度为k的串的前k/2的位置上的每一个字符在s中出现的次数,然后再找与之相对应的回文位置的字符在s中出现的次数,取max,2*n/k-max(*2是因为还要确保回文,所以相 对回文的位置也要修改)即是修改的次数,
注意:如果k是奇数,不要忘了最中间的那个字符,所以此时我们还要对中间位置k/2再找一次max,用原来的次数加上n/k-max(因为这次只找了一个位置,所以不用*2)就是答案了
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <map>
10 #include <set>
11 #include <unordered_set>
12 #include <unordered_map>
13 #define ll long long
14 #define fi first
15 #define se second
16 #define pb push_back
17 #define me memset
18 const int N = 1e6 + 10;
19 const int M = 1e9 + 7;
20 using namespace std;
21 typedef pair<int,int> PII;
22 typedef pair<long,long> PLL;
23
24 int t;
25 int n,k;
26 string s;
27 int cnt[26];
28 int main() {
29 ios::sync_with_stdio(false);
30 cin>>t;
31 while(t--){
32 cin>>n>>k>>s;
33 int ans=0;
34 for(int i=0;i<k/2;++i){
35 int ma=0;
36 me(cnt,0,sizeof(cnt));
37 for(int j=0;j<n/k;++j){
38 cnt[s[j*k+i]-'a']++;
39 ma=max(ma,cnt[s[j*k+i]-'a']);
40 cnt[s[j*k+(k-i-1)]-'a']++;
41 ma=max(ma,cnt[s[j*k+(k-i-1)]-'a']);
42 }
43 ans+=2*(n/k)-ma;
44 }
45
46 if(k%2==1){
47 me(cnt,0,sizeof(cnt));
48 int ma=0;
49 int i=k/2;
50 for(int j=0;j<n/k;++j){
51 cnt[s[j*k+i]-'a']++;
52 ma=max(ma,cnt[s[j*k+i]-'a']);
53 }
54 ans+=n/k-ma;
55 }
56 printf("%d\n",ans);
57 }
58
59
60
61 return 0;
62 }
Codeforces Round#630 div2 A~C题解的更多相关文章
- Codeforces Round #549 div2 1143-B Nirvana 题解
Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater v ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- 【Codeforces Round】 #431 (Div. 2) 题解
Codeforces Round #431 (Div. 2) A. Odds and Ends time limit per test 1 second memory limit per test ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
随机推荐
- 获取json格式的数据变成了undefined!?
今天在做一个简单的登陆功能时,当一切准备就绪,点击登陆时,什么都没发生..然后开始debug,打断点调试,然后发现了这个.向页面传递数据flag是true代表该用户的账号密码验证码等信息正确可以登录, ...
- Maven学习笔记之第一个Maven项目(Linux)
Maven是Apache旗下的管理Java项目jar包的项目管理工具,有了它可以很方便构建和管理我们的Java项目,你不必在互联网上逐个查找你需要的第三方jar包,你只需在maven reposito ...
- 【Linux】使用grep快速比较两个文件不同
两个文件的比较,会有同学说使用diff,和vimdiff就可以快速比较,为什么还要使用grep呢? 有些时候,diff和vimdiff的时候环境不符合,这样的情况,就可以使用grep来解决这个问题. ...
- 两节锂电池充电芯片,和保护IC的接法
1.两节锂电池的充电电路:可以分为三种方式. 第一种,USB口的5V输入,使用一颗SOT23-6的升压IC,直接升压到8.4V.电流在1A以下.优点是成本最低,缺点是,没有锂电池充电控制逻辑,和锂电池 ...
- 微人事项目-mybatis-持久层
摘要 最近将微人事这个开源项目进行了复现,这篇文章记录mybaits访问数据库这一块. 其中MyBatis是一个流行的持久层框架,支持自定义SQL.存储过程和高级映射.MyBatis消除了几乎所有的J ...
- Java中的深浅拷贝问题,你清楚吗?
一.前言 拷贝这个词想必大家都很熟悉,在工作中经常需要拷贝一份文件作为副本.拷贝的好处也很明显,相较于新建来说,可以节省很大的工作量.在Java中,同样存在拷贝这个概念,拷贝的意义也是可以节省创建对象 ...
- 解决windows与虚拟机ubuntu互相ping不通的问题
工作中经常用Ubuntu开发,而Ubuntu是安装在虚拟机中的,在弄网络开发的时候经常会用windows下的网络调试工具与Ubuntu中写好的网络程序进行通信,首先要保证windows与Ubuntu能 ...
- Ajax中的同源政策
Ajax中的同源政策 Ajax请求限制 Ajax只能向自己的服务器发送请求.比如现在有一个A网站.有一个B网站,A网站中的HTML文件只能向A网站服务器中发送Ajax请求,B网站中的HTML文件只能向 ...
- AWS Lightsail 开启 Root 登陆权限
将下面代码中的第一句中的 Passwd 改为自己将要设置的密码,否则默认 root 密码为 Passwd. #!/bin/bash echo root:Passwd |sudo chpasswd ro ...
- 判断2个list中是否有相同的数据(相交)Collections.disjoint
https://blog.csdn.net/yang_niuxxx/article/details/85092490 private void initData() { for (int i = 0; ...