A - Fox And Two Dots
http://codeforces.com/problemset/problem/510/B
#include<stdio.h>
#include<string.h>
char a[200][200];
int vis[200][200];
int n,m,flag;
int next[5][2]={{0,0},{0,1},{-1,0},{0,-1},{1,0}};
void dfs(int x,int y,int prx,int pry)
{
int i,j,tx,ty;
for(i=1;i<=4;i++)
{
tx=x+next[i][0];
ty=y+next[i][1];
if(tx>=0 && tx<n && ty>=0 && ty<m && a[tx][ty]==a[prx][pry] )
{
if(tx==prx && ty==pry)
{
continue;
}
if(vis[tx][ty]==1)
{
flag=1;
break;
}
vis[tx][ty]=1;
dfs(tx,ty,x,y);
}
if(flag==1)
break;
}
return ;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",a[i]);
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(vis[i][j]==1)
{
continue;
}
flag=0;
vis[i][j]=1;
dfs(i,j,i,j);
if(flag==1)
break;
}
if(flag==1)
break;
}
if(flag==1)
printf("Yes\n");
else
printf("No\n");
}return 0;
}
B - Saruman's Army
https://vjudge.net/contest/202187#problem/B
Description
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.
Input
The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.
Output
For each test case, print a single integer indicating the minimum number of palantirs needed.
Sample Input
0 3 10 20 20 10 7 70 30 1 7 15 20 50 -1 -1
Sample Output
2 4
Hint
In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.
In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int a[1050],b[1050],x;
int i,r,n;
while(~scanf("%d%d",&r,&n))
{
if(r==-1&&n==-1)
return 0;
int count=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
int p=a[0];
for(i=0;i<n;)
{
p=a[i++];
for(;i<n&&p+r>=a[i];i++);
p=a[i-1]; count++;
for(;i<n&&p+r>=a[i];i++);
}
printf("%d\n",count);
}
return 0;
}
C - Spreadsheets
http://codeforces.com/problemset/problem/1/B
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
Write n lines, each line should contain a cell coordinates in the other numeration system.
2 R23C55 BC23
BC23 R23C55
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include<math.h>
using namespace std;
int judge(char a[])
{
int i;
int len=strlen(a);
if(a[0]=='R')
{
for(i=0;i<len;i++)
{
if(isdigit(a[i])&&i>=1)
continue;
if(a[i]=='C'&&isdigit(a[i-1])&&isdigit(a[i+1]))
return 1;
}
}
return 0;
}
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
while(n--)
{
int n1,n2;
char a[200];
int b[200]={0};
char c[200]={'\0'};
scanf("%s",a);
if(judge(a))
{
sscanf(a,"R%dC%d",&n1,&n2);
i=0;
while(n2>0)
{
if(n2%26==0)
{
b[i++]=26;
n2--;
}
else
{
b[i++]=n2%26;
}
n2/=26;
}
for(i--;i>=0;i--)
{
printf("%c",b[i]+64);
}
printf("%d\n",n1);
}
else
{
sscanf(a,"%[A-Z]%d",c,&n2);
int len=strlen(c);
reverse(c,c+len);
n1=0;
for(i=0;i<strlen(c);i++)
{
n1+=((c[i]-'A'+1)*pow(26,i));
}
printf("R%dC%d\n",n2,n1);
}
}
}
return 0;
}
D - Find The Bone
http://codeforces.com/problemset/problem/796/B
#include<stdio.h>
int main()
{
int x,y,vis[10000000]={0};
int n,m,k,i,ans;
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<m;i++)
{
scanf("%d",&x);
vis[x]=1;
}
ans=1;
for(i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
if(x==ans)
{
if(vis[x]!=1)
ans=y;
}
else if (y==ans)
{
if(vis[y]!=1)
ans=x;
}
}
printf("%d\n",ans);
return 0;
}
E - Okabe and Banana Trees
http://codeforces.com/problemset/problem/821/B
#include<stdio.h>
#include<algorithm>
typedef long long ll;
using namespace std;
int main()
{
ll x,y;
ll b,m;
scanf("%lld%lld",&m,&b);
ll ans=0;
for(y=0;y<=b;y++)
{
x=m*(b-y);
ans=max(ans,((x*(x+1)/2*(y+1))+(y*(y+1)/2*(x+1))));
}
printf("%lld\n",ans);
return 0;
}
F - Sagheer and Nubian Market
https://vjudge.net/problem/839090/origin
#include<stdio.h>
#include<algorithm>
#define maxn 100055
using namespace std;
typedef long long ll;
ll s,n,a[maxn],b[maxn];
int judge(ll x)
{
int i;
for(i=0;i<n;i++)
{
b[i]=a[i]+((i+1)*x);
}
sort(b,b+n);
ll sum=0;
for(i=0;i<x;i++)
{
sum+=b[i];
}
if(sum<=s)
return 1;
else
return 0;
}
int main()
{
while(~scanf("%lld%lld",&n,&s))
{
int i;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
ll left=1,right=n,mid,ans=0;
while(left<=right)
{
mid=(left+right)/2;
if(!judge(mid))
right=mid-1;
if(judge(mid))
left=mid+1,ans=mid;
}
ll sum=0;
for(i=0;i<n;i++)
{
b[i]=a[i]+((i+1)*ans);
}
sort(b,b+n);
for(i=0;i<ans;i++)
{
sum+=(b[i]);
}
printf("%lld %lld\n",ans,sum);
}
return 0;
}
G - Mike and palindrome
http://codeforces.com/problemset/problem/798/A
Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
The first and single line contains string s (1 ≤ |s| ≤ 15).
Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
abccaa
YES
abbcca
NO
abcda
YES
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 20
using namespace std;
int main()
{
char a[N],b[N];
int i,j,len,sum=0;
scanf("%s",a);
len=strlen(a);
strncpy(b,a,sizeof(a));
reverse(b,b+len);
if(strcmp(a,b)==0)
{
if(len%2)
{
printf("YES\n");
return 0;
}
if(len%2==0)
{
printf("NO\n");
return 0;
}
}
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-1-i])
{
a[i]=a[len-1-i];
b[len-1-i]=b[i];
sum++;
}
}
if(strcmp(a,b)==0&&sum<=1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
H - Beautiful Divisors
https://vjudge.net/problem/1221716/origin
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[]={1,6,28,120,496,2016,8128,32640,130816};
int main()
{
int n,i,ans;
scanf("%d",&n);
for(i=0;i<9;i++)
{
if(n%a[i]==0)
ans=a[i];
}
printf("%d\n",ans);
return 0;
}
I - Rikka with Competition
http://acm.hdu.edu.cn/showproblem.php?pid=6095
A wrestling match will be held tomorrow. n players will take part in it. The i th player’s strength point is ai .
If there is a match between the i th player plays and the j th player, the result will be related to |ai−aj| . If |ai−aj|>K , the player with the higher strength point will win. Otherwise each player will have a chance to win.
The competition rules is a little strange. Each time, the referee will choose two players from all remaining players randomly and hold a match between them. The loser will be be eliminated. After n−1 matches, the last player will be the winner.
Now, Yuta shows the numbers n,K and the array a and he wants to know how many players have a chance to win the competition.
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains two numbers n,K(1≤n≤105,0≤K<109) .
The second line contains n numbers ai(1≤ai≤109) .
2 5 3 1 5 9 6 3 5 2 1 5 9 6 3
5 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define N 100050
using namespace std;
typedef long long ll;
int main()
{
ll a[N],K;
int n,ans,t,i,cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d%lld",&n,&K);
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n);
ans=a[n-1];
cnt=0;
for(i=n-2;i>=0;i--)
{
if(ans-a[i]>K)
{
cnt++;
break;
}
else
{
cnt++;
ans=a[i];
}
if(i==0)
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
J - Hacker, pack your bags!
http://codeforces.com/problemset/problem/822/C
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
4 5 1 3 4 1 2 5 5 6 1 1 2 4
5
3 2 4 6 3 2 4 1 3 5 4
-1
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define inf 1000000000000
using namespace std;
const int M = 2e5 + 10;
typedef long long ll;
struct TnT
{
int sta, cau, flag;
ll val;
} a[M << 1];
bool cmp(TnT x, TnT y)
{
if(x.sta == y.sta) return x.flag > y.flag;
return x.sta < y.sta;
}
ll dp[M];
int main()
{
int n,i;
ll x;
scanf("%d%lld",&n,&x);
int cnt=0;
for(i=0;i<n;i++)
{
int l,r;
ll c;
scanf("%d%d%lld",&l, &r , &c);
a[cnt].sta=l,a[cnt].cau=r-l+1,a[cnt].flag=1,a[cnt].val=c;
a[++cnt].sta=r,a[cnt].cau=r-l+1,a[cnt].flag=-1,a[cnt].val=c;
cnt++;
}
sort(a,a+cnt,cmp);
for(i=0;i<M;i++)
dp[i]=inf;
ll ans=inf;
for(i=0;i<cnt;i++)
{
if(a[i].flag==1)
{
ll sum=x-a[i].cau;
if(sum>=0)
{
if(dp[sum]<inf)
ans=min( ans, a[i].val + dp[sum] );
}
}
else
{
dp[a[i].cau]=min(dp[a[i].cau],a[i].val);
}
}
if(ans<inf)
printf("%lld\n",ans);
else
printf("-1\n");
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 100000000000
typedef long long ll;
using namespace std;
struct node
{
int l,r,c,value,cost;
}p[200005];
int cmp(node a,node b)
{
if(a.c==b.c)
return a.r<b.r;
return a.c<b.c;
}
int main()
{
int n,x,i,j,k;
scanf("%d%d",&n,&x);
for(i=0;i<n;i++)
{
scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].value);
p[i].c=p[i].r-p[i].l+1;
p[i].cost=p[i].value;
}
ll minz=inf;
sort(p,p+n,cmp);
for(i=0;i<n;i++)
{
if(i>0&&p[i].c==p[i-1].c)
p[i].cost=min(p[i].cost,p[i-1].cost);
}
for(i=0;i<n;i++)
{
if(p[i].c>=x)
continue;
ll temp=x-p[i].c,v=p[i].value;
int l=0,r=n-1,mid;
while(l<=r)
{
mid=(l+r)/2;
if(p[mid].c<temp)
{
l=mid+1;
}
else if (p[mid].c==temp)
{
if(p[i].l<=p[mid].r)
r=mid-1;
else
{
l=mid+1;
minz=min(minz,v+p[mid].cost);
}
}
else
r=mid-1;
}
}
if(minz==inf)
printf("-1\n");
else
printf("%lld\n",minz);
return 0;
}