AIM Tech Round (Div. 2)题解

A. Save Luke

Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v1 and v2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.

Input

The first line of the input contains four integers d, L, v1, v2 (1 ≤ d, L, v1, v2 ≤ 10 000, d < L) — Luke’s width, the initial position of the second press and the speed of the first and second presses, respectively.

Output

Print a single real value — the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let’s assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if 这里写图片描述.

Sample test(s)

Input
2 6 2 2
Output
1.00000000000000000000
Input
1 9 1 2
Output
2.66666666666666650000
Note
In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.
In the second sample he needs to occupy the position 这里写图片描述. In this case both presses move to his edges at the same time.


题意
Luke 在一个碎纸机上,碎纸机两端各有一个压力机,碎纸机长L,Luke 长d, 两个压力机会向里面移动,速度是v1和v2,问Luke 可以存活的最长时间
思路
简单水题,直接计算就行了

#include <bits/stdc++.h>
#define N 102
#define ll long long
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
//  freopen("1.txt", "r", stdin);
#endif
    int i, j, k;
    int v1, v2, d, l;
    cin >> d >> l >> v1 >> v2;
    double ans = 0;
    l -= d;
    ans = 1.0*l/(v1+v2+0.0);
    cout.setf(ios::fixed);
    cout << setprecision(10) << ans;
    return 0;
}

B. Making a String

You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:

the i-th letter occurs in the string no more than ai times;
the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.

Input

The first line of the input contains a single integer n (2  ≤  n  ≤  26) — the number of letters in the alphabet.

The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.

Output

Print a single integer — the maximum length of the string that meets all the requirements.

Sample test(s)

Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let’s consider an alphabet consisting of three letters: “a”, “b”, “c”. In the first sample, some of the optimal strings are: “cccaabbccbb”, “aabcbcbcbcb”. In the second sample some of the optimal strings are: “acc”, “cbc”.


题意
有n种字符,第i个字符有ai个,问由这些字符组成的字符串的最大长度,要求:
1. 字符串中每个字符出现的次数不能超过ai
2. 任何两种不同字符出现的次数不能相同
思路
C++中set容器的特点是:每一个数据只出现一次。(set详解
这道题使用set容器就会变得很简单。
具体代码如下:

#include <bits/stdc++.h>
#define N 30
#define ll long long
using namespace std;
int n, m, a[N];
int main()
{
#ifndef ONLINE_JUDGE
//  freopen("1.txt", "r", stdin);
#endif
    int i, j, k;
    cin >> n;
    ll ans = 0;
    set<int> ss;
    for(i = 0; i < n; i++)
    {
        cin >> a[i];
        while (a[i] > 0 && ss.find(a[i]) != ss.end())   a[i]--;
        ans += a[i];
        ss.insert(a[i]);
    }
    cout << ans;
    return 0;
}

C. Graph and String

One day student Vasya was sitting on a lecture and mentioned a string s1s2… sn, consisting of letters “a”, “b” and “c” that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties:

G has exactly n vertices, numbered from 1 to n.
For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs “a”-“b” and “b”-“c” are neighbouring, while letters “a”-“c” are not.

Vasya painted the resulting graph near the string and then erased the string. Next day Vasya’s friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya’s adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.

Input

The first line of the input contains two integers n and m 这里写图片描述 — the number of vertices and edges in the graph found by Petya, respectively.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.

Output

In the first line print “Yes” (without the quotes), if the string s Petya is interested in really exists and “No” (without the quotes) otherwise.

If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters “a”, “b” and “c” only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.

Sample test(s)

Input
2 1
1 2
Output
Yes
aa
Input
4 3
1 2
1 3
1 4
Output
No
Note
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings “aa”, “ab”, “ba”, “bb”, “bc”, “cb”, “cc” meets the graph’s conditions.

In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.


题意
一个图由一个长度为n的字符串s(这个字符串中仅含有’a’, ‘b’, ‘c’)生成,生成规则如下:
这个图中有n的顶点,对于任意的i≠j,顶点i和顶点j存在通路当且仅当 si和 sj相等或相邻

给出一个图的所有变,问是否存在一个对应的字符串
思路
当时做的时候没一点思路,后来百度题解才发现原来很简单
直接判补图,如果补图g[i][j]==1,则si和 sj一定有一个a,另一个是c。这相当于一个染色问题,对补图用a-c染色,对于单独的点直接染为b。但这样还不行,最后还需要验证一下所求出来的字符串是不是要求的字符串。

#include <bits/stdc++.h>
#define ll long long
#define N 505 
using namespace std;
int n, m, g1[N][N], in[N], g2[N][N];
char ans[N];
int dfs(int v, char c)
{
    ans[v] = c;
    for (int i = 1; i <= n; i++)
    {
        if (i == v) continue;
        if (ans[i]) continue;
        if (g1[v][i])
        {
            if (c == 'a')   dfs(i, 'c');
            else dfs(i, 'a');
        }
    }
}
bool chack()
{
    int i, j;
    memset(g2, 0, sizeof(g2));
    for (i = 1; i <= n; i++)
        for (j = 0; j <= n; j++)
        {
            if (i == j) continue;
            if (abs(ans[i]-ans[j]) == 2)    g2[i][j] = 1;
        }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i == j) continue;
            if (g1[i][j] != g2[i][j])   return false;
        }
    }
    return true;
}
int main()
{
//  freopen("1.txt", "r", stdin);
    int i, j, k, u, v;
    bool flag = false;
    scanf("%d%d", &n, &m);
    memset(ans, 0, sizeof(ans));
    for (i = 0; i <= n; i++)
    {
        in[i] = n-1;
        for (j = 0; j <= n; j++)
            g1[i][j] = 1;
    }
    for (i = 0; i < m; i++)
    {
        scanf("%d%d", &u, &v);
        g1[u][v] = g1[v][u] = 0;
        in[u]--;
        in[v]--;
    }
    for (i = 1; i <= n; i++)
    {
        if (!in[i]) ans[i] = 'b';
    }
    for (i = 1; i <= n; i++)
    {
        if (ans[i]) continue;
        dfs(i, 'a');
    }
    if (!chack())
    {
        printf("No\n");
    }
    else
    {
        ans[n+1] = 0;
        printf("Yes\n");
        puts(ans+1);
    }
    return 0;
}

D. Array GCD

You are given array ai of length n. You may consecutively apply two operations to this array:

remove some subsegment (continuous subsequence) of length m < n and pay for it m·a coins;
change some elements of the array by at most 1, and pay b coins for each change. 

Please note that each of operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.

Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than 1.

Input

The first line of the input contains integers n, a and b (1 ≤ n ≤ 1 000 000, 0 ≤ a, b ≤ 109) — the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.

The second line contains n integers ai (2 ≤ ai ≤ 109) — elements of the array.

Output

Print a single number — the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.

Sample test(s)

Input
3 1 4
4 2 3
Output
1
Input
5 3 2
5 17 13 5 6
Output
8
Input
8 3 4
3 7 5 4 3 12 9 4
Output
13
Note
In the first sample the optimal way is to remove number 3 and pay 1 coin for it.

In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to 2·3 + 2 = 8 coins.


题意
给一个数组,要求经过一些操作,使这个数组的所有数的最大公约数大于1。
所能进行的两种操作如下
1. 去除一个长度为m(m<n)的子串,花费是m*a个硬币
2. 改变某些数的值(加1、或减1),每改变一个值花费b个硬币
以上两种操作每种最多用1次,问花费的硬币最少的是多少
思路
由题意克制,显然至少数组会留下第一个数或着最后一个数
那么x[1]、x[1]+1、x[1]-1、x[n]、x[n]+1、x[n]-1这六个数中必有一个存在题解数组里,计算出他们所有的质因子,那么最后解数组的最小公约数就在这些数之中,对这些质因子一一进行判断,求所花费代价的最小值就行了
记f[i]为从左到右进行到i位置,每次只进行2操作或不进行的代价
记g[j]为从右到左进行到j位置,每次只进行2操作或不进行的代价
那么答案为f[i]+(j-i-1)*b+g[j]=(f[i]-(i+1)*b) + (g[j]+j*b)

复杂度o(n*质因子个数)=o(n*log(1e9)) .
代码如下:

#include <bits/stdc++.h>
#define ll long long
#define N 1000100 
using namespace std;
const ll MAX=1e18+10;
ll  n, a, b;
ll x[N], f[N], g[N];
//f[i]为从左到右进行到i位置,每次只进行2操作或不进行的代价
//g[i]为从右到左进行到i位置,每次只进行2操作或不进行的代价
vector<ll> prime;
bool isprime[N];
set<ll> s;
void getPrime() //计算出0~N里面所有的素数
{
    int i, j;
    for (i = 0; i < N; i++)
        isprime[i] =true;
    isprime[0] = isprime[1] = 0;
    for (i = 2; i < N; i++)
    {
        if (!isprime[i])    continue;
        for (j = 2*i; j < N; j += i)
            isprime[j] = false;
    }
    prime.clear();
    for (i = 2; i < N; i++)
        prime.push_back(i);
}

ll change(ll a, ll p)
{
    if (a%p == 0)   return 0;
    if ((a+1)%p==0 || (a-1)%p==0)   return b;
    return -1;
}
ll Solve(ll p)
{
    int L = n+1, R = 0, i, tag = 0;
    ll t, G, res;
    f[0] = 0;
    for (i = 1; i <= n; i++)
    {
        t = change(x[i], p);
        if (t == -1)
        {
            L = i;
            tag = 1;
        }
        if (tag)    f[i] = MAX;
        else f[i] = f[i-1]+t;
    }
    g[n+1] = 0;
    tag = 0;
    for (i = n; i > 0; i--)
    {
        t = change(x[i], p);
        if (t == -1)
        {
            R = i;
            tag = 1;
        }
        if (tag)    g[i] = MAX;
        else g[i] = g[i+1]+t;
    }
    G = g[n+1]+(n+1)*a;
    res = f[n];
    for (i = n; i >= 0; i--)
    {
        res = min(res, f[i]-(i+1)*a+G);
        G = min(G, g[i]+i*a);
    }
    return res;
}
void Get(ll a)
{
    if (a == 0 || a == 1)   return ;
    int i, l = prime.size(), t;
    for (i = 0; i < l; i++)
    {
        t = prime[i];
        if (t*t > a)    break;
        while(a%t == 0)
        {
            if (s.find(t) == s.end())
                s.insert(t);
            a /= t;
        }
    }
    if (a > 1)  s.insert(a);
}
void Init()
{
    s.clear();
    Get(x[1]);
    Get(x[1]+1);
    Get(x[1]-1);
    Get(x[n]);
    Get(x[n]+1);
    Get(x[n]-1);
}
int main()
{
//  freopen("1.txt", "r", stdin);
    int i, j;
    ll  ans;
    set<ll>::iterator it;
    getPrime();
    scanf("%I64d%I64d%I64d", &n, &a, &b);
    for (i = 1; i <= n; i++)
        scanf("%I64d", &x[i]);
    Init();
    ans = MAX;
    for (it = s.begin(); it != s.end(); it++)
        ans = min(ans, Solve(*it));
    printf("%I64d", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值