A. Smart AEMShana!
单点时限: 2.0 sec
内存限制: 512 MB
AEMShana isn’t just charming, he is also very smart.
While some of us were learning the addition table, AEMShana had fun in his own manner.
AEMShana painted an mXn addition table, where the element on the intersection of the -th row and -th column equals i+j (the rows and columns of the table are numbered starting from 1).
Then he was asked: what number in the table is the k-th least number? AEMShana always answered correctly and immediately. Can you repeat his success?
Consider the given addition table. If you write out all mXn numbers from the table in the decreasing order, then the k-th number you write out is called the k-th leaest number.
Note
A addition table looks like this:
2 3 4
3 4 5
4 5 6
输入格式
There are multiple test cases(not more than ) in this problem.
In each test case contains integers and .
输出格式
Print the -th least number in a addition table.
样例
input
3 4
114514 1919810
output
4
1960
推公式+二分
2有1个,3有2个,4有3个
然后到1+m
有m个
2+m有m-1个
就照这样推~
#include<iostream>
#include<cmath>
using namespace std;
typedef unsigned long long ull;
ull m,k,l,r,x,ll,rr;
int main()
{
while(cin>>m>>k)
{
int flag=0;
ull t=m*2;
ull l=2,r=m+1;
while(l<r)
{
x=(l+r)/2;
ll=(x*x-3*x+4)/2,rr=(x*x-x)/2;
if(rr>=k) r=x;
else l=x+1;
}
ll=(l*l-3*l+4)/2,rr=(l*l-l)/2;
if(k>=ll&&k<=rr)
{
cout<<l<<endl;
flag=1;
}
if(flag==0)
{
l=m+2,r=2*m;
while(l < r)
{
ull x=l+r>>1;
ull ll=m*m-(t-x+1)*(t-x)/2-t+x,rr=m*m-(t-x+1)*(t-x)/2;
if(rr>=k) r=x;
else l=x+1;
}
cout<<l<<endl;
}
}
return 0;
}
C. Back
单纯的DFS过不了,需要用并查集判环。或者搜个生成树然后向起点连条边也可以。
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5000;
int fa[10100];
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline string readstring() {
string str;
char s=getchar();
while(s==' '||s=='\n'||s=='\r'){s=getchar();}
while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}
return str;
}
int Find(int x) {
if(fa[x] == x) return x;
return fa[x] = Find(fa[x]);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=0; i<N; i++) fa[i] = i;
int flag = 0;
while(m--)
{
int x = read();
int y = read();
x = Find(x);
y = Find(y);
if(x==y) flag = 1;
else fa[y] = x;
}
if(flag) puts("Yes");
else puts("No");
}
}
H. A Simple Game
单点时限: 1.0 sec
内存限制: 512 MB
This quesiton is very simple.This quesiton is very simple.This quesiton is very simple.
Alice and Bob play a game. Initially they have n cards. They take alternating turns, and Alice is moving first.
During each turn, the player will choose a certain card and get points. If Alice chooses the i-th card, her score will be increased by ai. If Bob chooses the i-th card,his score will be increased by bi. Each card can only be choosed by one player. The game ends when all cards are choosed.
Whoever gets a higher score, wins. You have to determine who wins if they both play optimally.
输入格式
There are multiple test cases in this problem.
In each test case,the first line contains one integer n(1<=n<=1e5). The second line contains space-separated positive integers ai(1<=ai<=1e9). The third line contains space-separated positive integers bi(1<=bi<=1e9).
输出格式
For each test case print Alice if Alice can win and Bob if Bob can win.If they get the same score,print AEMSHANA.
样例
input
3
1 2 3
4 5 6
2
1 8
9 1
2
3 5
4 1
output
Bob
AEMSHANA
Alice
一眼就相中了这条博弈论,算是出的比较快的
定义一个 a[i]+b[i] 的和 作辅助用
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct Ran {
int a, b, s;
}q[1000010];
int a[1000010], b[1000010];
bool cmp(Ran a, Ran b) {return a.s>b.s;}
int main()
{
int n;
while(cin>>n)
{
for(int i=1; i<=n; i++) scanf("%d", &a[i]);
for(int i=1; i<=n; i++) scanf("%d", &b[i]);
for(int i=1; i<=n; i++) {
q[i].a = a[i];
q[i].b = b[i];
q[i].s = a[i]+b[i];
}
sort(q+1, q+1+n, cmp);
ll xx=0, xy=0;
for (int i=1; i<=n; i++){
if(i%2) xx += q[i].a;
else xy += q[i].b;
}
if(xx>xy) cout<<"Alice"<<endl;
if(xx==xy) cout<<"AEMSHANA"<<endl;
if(xx<xy) cout<<"Bob"<<endl;
}
return 0;
}