一、Wlacm
1.曲径通幽
题意:
在N×N的方格中填入正整数1~N^2,可以有很多种方案。 如果按照迂回的路线(如下图)填数,你能给出比较有效的解法吗?
输入:
正整数N(≤30)
输出:
1~N^2构成的N×N数值方阵,输出时每个数值占4个位置。
样例输入:
1
2
3
4
样例输出:
1
1 4
2 3
1 4 5
2 3 6
9 8 7
1 4 5 16
2 3 6 15
9 8 7 14
10 11 12 13
解题思路:
其实我本来是不会写这道题的,后来别的题写不出来就开始写这道题。刚开始我真的毫无思绪,后来就开始找规律,发现了它走位的小规律,就开始模拟,居然过了,但是应该写的不是很规范。
程序代码:(my_AC)
#include<bits/stdc++.h>
using namespace std;
int res[1000][1000];
int main(){
int n;
while(~scanf("%d",&n)){
int cnt=4,x=1,y=3;
res[1][1]=1;
res[2][1]=2;
res[2][2]=3;
res[1][2]=4;
int num=1;
if(n<=2){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%4d",res[i][j]);
}
printf("\n");
}
continue;
}
for(int i=1;i<=(n+1)/2;i++){
// cout<<1<<endl;
res[x][y]=++cnt;x++;
int temp=x+num;
while(x<=temp)
res[x++][y]=++cnt;y--;x--;
temp=y-num;
num++;
while(y>=temp)
res[x][y--]=++cnt;y++;x++;
temp=y+num+1;
while(y<=temp)
res[x][y++]=++cnt;y--;x--;
temp=x-num;
while(x>=temp)
res[x--][y]=++cnt;y++;x++;
num++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%4d",res[i][j]);
}
printf("\n");
}
}
return 0;
}
程序代码:
#include"bits/stdc++.h"
using namespace std;
int map1[1000][1000];
int main(){
int n;
cin>>n;
map1[1][1]=1;
map1[2][1]=2;
int num=3,x=2,y=1;
for(int i=1;i<n;i++){
int temp=i;
if(i%2==1){
temp=i;
while(temp--){map1[x][++y]=num++;}
temp=i;
while(temp--){map1[--x][y]=num++;}
map1[x][++y]=num++;
}else {
temp=i;
while(temp--){map1[++x][y]=num++;}
temp=i;
while(temp--){map1[x][--y]=num++;}
map1[++x][y]=num++;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%4d",map1[i][j]);
}
printf("\n");
}
return 0;
}
2.Rectangle Filling
题意:
给出一个大小为 n × m 的矩形,现用大小为 a × a 的正方形填充该矩形,且要求正方形的边必须和矩形的边平行。求至多能填入多少正方形。
注意:正方形可以正好碰到矩形边界,但不能超出矩形外,且正方形不可重叠。
输入:
本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 100),表示数据组数,对于每组数据:
第一行有三个整数 n, m, a (1 ≤ n, m, a ≤ 109),意义如上所述。
输出:
每组数据输出一行一个整数,表示至多能填入多少正方形。
样例输入:
3
7 7 3
8 4 2
4 7 5
样例输出:
4
8
0
程序代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m,a;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%lld %lld %lld",&n,&m,&a);
if(a>n||a>m){
cout<<0<<endl;
continue;
}
long long t1=n/a;
long long t2=m/a;
cout<<t1*t2<<endl;
}
return 0;
}
3.Group the Integers
题意:
将前 n 个正整数分成 k 组,使得每组中的正整数两两互质。求 k 的最小值。
注意:两个正整数互质,指的是两个正整数的最大公因数等于 1。
输入:
本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 105),表示数据组数,对于每组数据:
第一行只有一个整数 n (1 ≤ n ≤ 109),意义如上所述。
提示:本题输入数据较大,请使用较为快速的输入输出方式。例如,使用 C++ 的同学可以使用 scanf/printf 代替 cin/cout。
输出:
每组数据输出一行一个整数,表示 k 的最小值。
样例输入:
2
3
4
样例输出:
1
2
解题思路:
刚开始我根本不知道怎么算,就一直没写出来,比赛完了,告诉我答案居然是n/2…无语了,找规律大赛!!
程序代码:
#include<bits/stdc++.h>
using namespace std;
long long n;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
if(n==1){
printf("1\n");
}else{
printf("%lld\n",n/2);
}
}
return 0;
}
4.Arithmetic Progression
题意:
给出 n 个整数 a1, a2, …, an,现在请你再加入一个整数 x,使得这 n + 1 个整数可以构成一个等差数列。求所有符合要求的 x。
输入:
本题有多组测试数据。第一行有一个整数 T (1 ≤ T ≤ 10),表示数据组数,对于每组数据:
第一行只有一个整数 n (1 ≤ n ≤ 50),意义如上所述。
第二行有 n 个整数 a1, a2, …, an (-108 ≤ ai ≤ 108),表示给出的整数。
输入数据保证每组数据至少能找到一个满足要求的整数 x。
输出:
每组数据输出两行。第一行输出一个整数,表示符合要求的整数 x 的数量。第二行按升序输出所有符合要求的整数 x,两个数之间用一个空格分隔。
如果该组数据有无数个整数 x 满足要求,则在第一行和第二行都输出 “INF”(不含引号)。
请不要在行末输出多余空格!
样例输入:
2
4
1 3 5 7
1
0
样例输出:
2
-1 9
INF
INF
解题思路:
这道题真是说起来一把辛酸泪,比赛的时候我想错了,就一直没写出来,回来补题,我已经知道为什么了,但是我的第一种方法就一直错,把我气坏了,这道题我改了三个晚上,每天在气愤中入睡,气死我了。后来用了第二种方法才过,但是我还是不知道为什么我第一种方法错了!
正确的方法就是:找到中间差值最大的地方。就是要插入的地方,如果没有就在两边,但是要考虑差值等于1时只有一个,其他有两个。n=2时要注意有3种情况,一定都要考虑到,刚开始只考虑了两个就一直WA。注意,n=1时是无穷多的。
程序代码:(AC)
#include<bits/stdc++.h>
using namespace std;
long long a[100];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
if(n==1){
printf("INF\nINF\n");
continue;
}
sort(a+1,a+n+1);
long long temp=a[2]-a[1],temp1=a[2]-a[1];
int d=1,flag1=0;
for(int i=2;i<n;i++){
long long t=a[i+1]-a[i];
if(t!=temp){
flag1=1;
}
if(temp<t){
temp1=temp;//temp1常规等差
temp=t;//temp是需要插入的等差
d=i;
}
if(temp>t) temp1=t;
}
if(n==2){
if(a[1]==a[2]){
printf("1\n%lld\n",a[1]);
}else if(temp%2==0){
printf("3\n");
printf("%lld %lld %lld\n",a[1]-temp,a[1]+temp/2,a[2]+temp);
}else if(temp%2==1){
printf("2\n");
printf("%lld %lld\n",a[1]-temp,a[2]+temp);
}
}else if(flag1==1){
printf("1\n");
printf("%lld\n",a[d]+temp1);
}else if(flag1==0){
if(temp==0){
printf("1\n%lld\n",a[1]);
}else{
printf("2\n%lld %lld\n",a[1]-temp,a[n]+temp);
}
}
}
return 0;
}
程序代码:(WA)
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int a[55];
int ans[5];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
if(n==1){
printf("INF\nINF\n");
continue;
}
int cnt=2;
sort(a+1,a+n+1);
int temp=INF,temp1=INF;
int flag=0,flag1=0,flag2=0;
for(int i=1;i<n;i++){
if(i==1){
temp=a[i+1]-a[i];
}else{
int t=a[i+1]-a[i];
if(temp!=t) flag2=1;
if(flag1==1){
if(t==temp1){
temp=temp1;
cnt++;
ans[cnt]=(a[2]+a[1])/2;
break;
}else{
cnt++;
ans[cnt]=(a[i]+a[i-1])/2;
break;
}
}
if(t!=temp && flag==1){
cnt++;
ans[cnt]=(a[i+1]+a[i])/2;
break;
}else if(t!=temp && flag==0){
temp1=t;flag1=1;
}else{
flag=1;
}
}
}
if(n==3 && (flag1)){
printf("1\n");
if(a[1]+temp1<a[2]){
printf("%d\n",a[1]+temp1);
}else{
printf("%d\n",a[2]+temp);
}
}else if(flag2==1){
printf("1\n%d\n",ans[3]);
}else if(n==2&&temp==2){
printf("3\n");
ans[1]=a[1]-temp;ans[2]=a[2]+temp;
printf("%d %d %d\n",ans[1],a[2]-1,ans[2]);
}else if(n==2){
if(temp%2==0){
printf("3\n");
printf("%d %d %d\n",a[1]-temp,(a[1]+a[2])/2,a[2]+temp);
}else{
printf("2\n");
printf("%d %d\n",a[1]-temp,a[2]+temp);
}
}else{
ans[1]=a[1]-temp;ans[2]=a[n]+temp;
sort(ans+1,ans+cnt+1);
if(ans[2]==ans[1])cnt--;
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++){
if(i==1)
printf("%d",ans[i]);
else{
if(ans[i]==ans[1]){
continue;
}else{
printf(" %d",ans[i]);
}
}
}
printf("\n");
}
}
return 0;
}
二、Atcoder
1.ROT N
题意:
We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.
Shift each character of S by N in alphabetical order (see below), and print the resulting string.
We assume that A follows Z. For example, shifting A by 2 results in C (A → B → C), and shifting Y by 3 results in B (Y → Z → A → B).
输入:
Input is given from Standard Input in the following format:
N
S
输出:
Print the string resulting from shifting each character of S by N in alphabetical order.
样例输入1:
2
ABCXYZ
样例输出1:
CDEZAB
Note that A follows Z.
样例输入2:
0
ABCXYZ
样例输出2:
ABCXYZ
样例输入3:
13
ABCDEFGHIJKLMNOPQRSTUVWXYZ
样例输出3:
NOPQRSTUVWXYZABCDEFGHIJKLM
解题思路:
就是给你一个数字和字符串,要求将字符串的每一个字符向右移n位,然后输出。
用char数组存即可
程序代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
char ans[N];
int main(){
int n;
string s;
cin>>n;
cin>>s;
int len=s.length();
for(int i=0;i<len;i++){
int temp=s[i]+n;
if(temp>90){
temp=temp-26;
}
ans[i]=temp;
}
// for(int i=0;i<len;i++)
cout<<ans<<endl;
// cout<<endl;
return 0;
}
2.Buy an Integer
题意:
Takahashi has come to an integer shop to buy an integer.
The shop sells the integers from 1 through 10^9. The integer N is sold for A×N+B×d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.
Find the largest integer that Takahashi can buy when he has X yen. If no integer can be bought, print 0.
注意:
All values in input are integers.
1≤A≤10^9
1≤B≤10^9
1≤X≤10^18
输入:
Input is given from Standard Input in the following format:
A B X
输出:
Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.
样例输入1:
10 7 100
样例输出1:
9
样例输入2:
2 1 100000000000
样例输出2:
1000000000
样例输入3:
1000000000 1000000000 100
样例输出3:
0
样例输入4:
1234 56789 314159265
样例输出4:
254309
解题思路:
给你a、b、x,a让你求A×N+B×d(N)=x,所满足的最大的N,d(N)代表N的位数。
因为N太大了。所以我写的代码超时了,而且最后两个点还WA了,我去看了别人写的代码,他们是用二分的方法写的,其实我也想到了二分,但是我没写出来,我气!!
程序代码:(AC)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int dig(LL x) //算位数
{
int ans = 0;
while(x != 0){
x /= 10;
ans++;
}
return ans;
}
int main()
{
LL a,b,x;
cin >> a >> b >> x ;
int l = 1,r = 1e9;
if(x >= 1e9)
r = 1e9;
else
r = x;
int mid;
while(l < r){
mid = (l + r) / 2;
LL c = mid * a + dig(mid) * b;
if(c <= x)
l = mid + 1;
else
r = mid;
}
if(l * a + dig(l) * b > x)
l--;
cout << l <<endl;
return 0;
}
三、Vjudge
1.ID (AtCoder - 4267 )
题意:
In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.
City i is established in year Yi and belongs to Prefecture Pi.
You can assume that there are no multiple cities that are established in the same year.
It is decided to allocate a 12-digit ID number to each city.
If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is Pi, and the last six digits of the ID number is x.
Here, if Pi or x (or both) has less than six digits, zeros are added to the left until it has six digits.
Find the ID numbers for all the cities.
Note that there can be a prefecture with no cities.
注意:
1≤N≤10^5
1≤M≤10^5
1≤Pi≤N
1≤Yi≤10^9
Yi are all different.
All values in input are integers.
输入:
Input is given from Standard Input in the following format:
N M
P1 Y1
:
PM YM
输出:
Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, …).
样例输入1:
2 3
1 32
2 63
1 12
样例输出1:
2 3
1 32
2 63
1 12
As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.
As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.
As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.
样例输入2:
2 3
2 55
2 77
2 99
样例输出2:
000002000001
000002000002
000002000003
解题思路:
题意就是第一个数字代表他所属的类别,第二个代表他的年份,你要判断他在他所属类别中的排名,然后输出他所属的类别和排名,每个占6位。
看着挺简单的,但是写的时候还是有点难,最后还是问了zmh才知道怎么写。
要先记录他们的序号,然后先按年份排序,然后得出他们的排名,最后再按序号排序,就又回到原来的顺序了,就可以输出了。
程序代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node{
int x,y;
int index,pre;
}a[N];
bool cmp(node a,node b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
bool cmp1(node a,node b){
return a.index<b.index;
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d",&a[i].x,&a[i].y);
a[i].index=i;
}
sort(a+1,a+m+1,cmp);
int cnt=1;
int t=a[1].x;
for(int i=1;i<=m;i++){
if(a[i].x != t){
t = a[i].x;
cnt = 1;
a[i].pre = cnt;
cnt++;
}
else{
a[i].pre = cnt;
cnt++;
}
}
sort(a+1,a+m+1,cmp1);
for(int i=1;i<=m;i++){
printf("%06d%06d\n",a[i].x,a[i].pre);
}
return 0;
}
2.Cave Painting (CodeForces - 922C )
题意:
Imp is watching a documentary about cave painting.
Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.
Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all , 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:
1 ≤ i < j ≤ k,
, where
is the remainder of division x by y.
输入:
The only line contains two integers n, k (1 ≤ n, k ≤ 10^18).
输出:
Print “Yes”, if all the remainders are distinct, and “No” otherwise.
You can print each letter in arbitrary case (lower or upper).
样例输入1:
4 4
样例输出1:
No
样例输入2:
5 3
样例输出2:
Yes
解题思路:
题意就是n取余1-k,有没有相同的余数,有就输出No,没有就输出Yes。
这个数也太大了,后来发现就是n%i!=(i-1),暴力就行了。
程序代码:
#include<iostream>
using namespace std;
long long n,t;
int main(){
scanf("%lld %lld",&n,&t);
for(int i=1;i<=t;i++){
if(n%i!=(i-1)){
printf("No\n");
return 0;
}
}
printf("Yes\n");
return 0;
}
3.Tricky Alchemy (CodeForces - 912A )
题意:
During the winter holidays, the demand for Christmas balls is exceptionally high. Since it’s already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.
Grisha needs to obtain some yellow, green and blue balls. It’s known that to produce a yellow ball one needs two yellow crystals, green — one yellow and one blue, and for a blue ball, three blue crystals are enough.
Right now there are A yellow and B blue crystals in Grisha’s disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.
输入:
The first line features two integers A and B (0 ≤ A, B ≤ 109), denoting the number of yellow and blue crystals respectively at Grisha’s disposal.
The next line contains three integers x, y and z (0 ≤ x, y, z ≤ 109) — the respective amounts of yellow, green and blue balls to be obtained.
输出:
Print a single integer — the minimum number of crystals that Grisha should acquire in addition.
样例输入1:
4 3
2 1 1
样例输出1:
2
样例输入2:
3 9
1 1 3
样例输出2:
1
样例输入3:
12345678 87654321
43043751 1000000000 53798715
样例输出3:
2147483648
解题思路:
就是黄球需要两个黄色的晶体,绿球需要一个黄色和一个蓝色,蓝球需要三个蓝色晶体。已知有a种黄色,b种蓝色,需要黄、绿、蓝球分别为x、y、z,问还需要多少晶体才可以。
模拟就行啦
程序代码:
#include<iostream>
using namespace std;
long long a,b,x,y,z;
long long ans=0,temp;
int main(){
scanf("%lld %lld %lld %lld %lld",&a,&b,&x,&y,&z);
temp=x*2;
if(a<temp){
ans+=(temp-a);
a=0;
}else{
a=a-temp;
}
if(a<y){
ans+=(y-a);
a=0;
}else{
a=a-y;
}
if(b<y){
ans+=(y-b);
b=0;
}else{
b=b-y;
}
temp=z*3;
if(b<temp){
ans+=(temp-b);
b=0;
}else{
b=b-temp;
}
printf("%lld\n",ans);
return 0;
}