HackerRank
A - Hanging Posters
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1<<21;
const int M = 1e3 + 10;
using namespace std;
int w[N];
int l[N];
void solve(){
int n,h;
cin>>n>>h;
rep(i,1,n){
cin>>w[i];
}
rep(i,1,n){
cin>>l[i];
}
int maxn=-1;
rep(i,1,n){
double res=w[i]*1.0-(l[i]*0.25);
maxn=max(maxn,(int)ceil(res)-h);
}
maxn=max(0,maxn);
cout<<maxn<<endl;
}
int main()
{
solve();
return 0;
}
C - Save the Queen!
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1e4 + 10;
const int M = 111;
using namespace std;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
int n,k;
int dp[N];
bool judge(double mid){
int res=n;
double sum=0;
rep(i,1,k){
if(dp[i]>=mid) res--;
else sum+=dp[i];
}
return sum>=mid*res;
}
void solve(){
cin>>n>>k;
rep(i,1,k){
cin>>dp[i];
}
double l=0;
double r=1e12;
rep(i,0,M){
double mid=(l+r)/2;
if(judge(mid)) l=mid;
else r=mid;
}
printf("%.9f\n",l);
}
int main()
{
solve();
return 0;
}
E - Video Conference
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1e4 + 10;
const int M = 111;
using namespace std;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
map<string,int> mp;
char s[15];
void solve(){
//ios::sync_with_stdio(0);
int n;
scanf("%d",&n);
rep(i,0,n-1){
scanf("%s",s);
string sub=s;
if(mp.count(sub)&&mp[sub]){
mp[sub]++;
printf("%s %d\n",sub.c_str(),mp[sub]);
}
else if(mp.count(sub)&&!mp[sub]){
printf("%s\n",sub.c_str());
mp[sub]++;
}
else{
string str;
bool flag=0;
rep(i,1,(int)sub.size()-1){
str=sub.substr(0,i);
if(!mp.count(str)){
if(!flag){
printf("%s\n",str.c_str());
flag=1;
}
mp[str]=0;
}
}
if(!flag)
printf("%s\n",sub.c_str());
mp[sub]=1;
}
}
}
int main()
{
solve();
return 0;
}
H - Customized Chess Board
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1<<21;
const int M = 111;
using namespace std;
int n;
int dp[M][M];
bool vis[M][M];
bool flag;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
void judge(){
rep(i,1,n){
rep(j,2,n){
if(dp[i][j]==dp[i][j-1]) {flag=1;return ;}
}
}
rep(i,1,n){
rep(j,2,n){
if(dp[j][i]==dp[j-1][i]) {flag=1;return ;}
}
}
}
void solve(){
int t;
cin>>t;
while(t--){
cin>>n;
memset(vis,0,sizeof(vis));
rep(i,1,n){
rep(j,1,n){
cin>>dp[i][j];
}
}
flag=0;
//DFS(1,1);
judge();
if(flag) puts("No");
else puts("Yes");
}
}
int main()
{
solve();
return 0;
}
L - Clock Delay
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1<<21;
const int M = 1e3 + 10;
using namespace std;
void solve(){
int t;
cin>>t;
while(t--){
int h1,m1;
int h2,m2;
cin>>h1>>m1>>h2>>m2;
int d;
cin>>d;
int ans=h1*60+m1-h2*60-m2+d*60;
cout<<ans<<endl;
}
}
int main()
{
solve();
return 0;
}
G - Array Partition (补题)
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
using namespace std;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }
int a[N];
int fa[N];
int p[1000004];
int prime[1000004];
int tot;
bool vis[1000005];
void init() {
for (int i = 2; i <= 1000000; i++) {
if (!vis[i]) {
prime[++tot] = i;
}
for (int j = 1; j <= tot &&(ll) i*(prime[j]) <= 1000000; j++) {
vis[i*prime[j]] = 1;
if (i%prime[j] == 0)break;
}
}
}
int findfa(int x) {
if (x == fa[x])return x;
else return fa[x] = findfa(fa[x]);
}
int qpow(int a, int b, int mod) {
int ans = 1;
while (b>0) {
if (b & 1)ans = (ll)ans * a%mod;
a =(ll) a * a%mod; b >>= 1;
}
return ans;
}
int main() {
init();
int T; cin >> T;
while (T--) {
int n; rdint(n);
int cnt = 0;
for (int i = 1; i <= n; i++) {
rdint(a[i]);
if (a[i] == 1) {
cnt++; n--; i--;
}
}
if (n) {
sort(a + 1, a + 1 + n);
n = unique(a + 1, a + 1 + n) - a - 1;
memset(p,0,sizeof(p));
for (int i = 1; i <= n; i++) {
p[a[i]] = i; fa[i] = i;
}
for (int i = 1; i <= tot; i++) {
int pp = 0;
for (int j = prime[i]; j <= 1000000; j += prime[i]) {
if (p[j]) {
if (!pp)pp = findfa(p[j]);
else {
int q = findfa(p[j]);
fa[q] = pp;
}
}
}
}
for (int i = 1; i <= n; i++) {
findfa(i);
if (i == fa[i])cnt++;
}
}
cout << (qpow(2, cnt, mod) - 2 + mod) % mod << endl;
}
return 0;
}