9-2(找规律,思路,

来源:互联网 发布:网络布线设计费怎么算 编辑:程序博客网 时间:2024/06/07 10:00

Odds and Ends

time limit per test1 second
memory limit per test256 megabytes

Where do odds begin, and where do they end? Where does hope emerge, and will they ever break?

Given an integer sequence a1, a2, …, an of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers.

A subsegment is a contiguous slice of the whole sequence. For example, {3, 4, 5} and {1} are subsegments of sequence {1, 2, 3, 4, 5, 6}, while {1, 2, 4} and {7} are not.

Input

The first line of input contains a non-negative integer n (1 ≤ n ≤ 100) — the length of the sequence.

The second line contains n space-separated non-negative integers a1, a2, …, an (0 ≤ ai ≤ 100) — the elements of the sequence.

Output

Output “Yes” if it’s possible to fulfill the requirements, and “No” otherwise.

You can output each letter in any case (upper or lower).

Examples

input
3
1 3 5
output
Yes
input
5
1 0 1 5 1
output
Yes
input
3
4 3 1
output
No
input
4
3 9 9 3
output
No

Note

In the first example, divide the sequence into 1 subsegment: {1, 3, 5} and the requirements will be met.

In the second example, divide the sequence into 3 subsegments: {1, 0, 1}, {5}, {1}.

In the third example, one of the subsegments must start with 4 which is an even number, thus the requirements cannot be met.

In the fourth example, the sequence can be divided into 2 subsegments: {3, 9, 9}, {3}, but this is not a valid solution because 2 is an even number.

思路题

#include <bits/stdc++.h>using namespace std;int main(){    int n;    bool flag = true;    while(cin >> n){        if(n%2)            flag = true;        else            flag = false;        for(int i = 0; i < n; i ++){            int t;            cin >> t;            if(i == 0 || i == n-1 )                if(t%2 == 0) flag = false;        }        if(flag)            printf("yes\n");        else            printf("no\n");    }    return 0;   }

别人更加优美的代码:

#include <cstdio>static const int MAXN = 102;int n;int a[MAXN];int main(){    scanf("%d", &n);    for (int i = 0; i < n; ++i) scanf("%d", &a[i]);    puts((n % 2 == 1) && (a[0] % 2 == 1) && (a[n - 1] % 2 == 1) ? "Yes" : "No");    return 0;}

Tell Your World

/*time limit per test1 second
memory limit per test256 megabytes*/

Connect the countless points with lines, till we reach the faraway yonder.

There are n points on a coordinate plane, the i-th of which being (i, yi).

Determine whether it’s possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.

Input

The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.

The second line contains n space-separated integers y1, y2, …, yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.

Output

Output “Yes” (without quotes) if it’s possible to fulfill the requirements, and “No” otherwise.

You can print each letter in any case (upper or lower).

Examples

input
5
7 5 8 6 9
output
Yes
input
5
-1 -2 0 0 -5
output
No
input
5
5 4 3 2 1
output
No
input
5
1000000000 0 0 0 0
output
Yes

Note

In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It’s possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.

In the second example, while it’s possible to draw two lines that cover all points, they cannot be made parallel.

In the third example, it’s impossible to satisfy both requirements at the same time.

注意两处特判

#include <bits/stdc++.h>using namespace std;pair<double, double> pii[1010];int app[1010];int n;bool judge(int app[], double k){    int mark;    for(int i = 1; i < n; i++)        if(!app[i]) {mark = i; break;}    for(int i = mark+1; i < n; i++){        if(!app[i])            if((pii[i].second - pii[mark].second)/ (i - mark) != k) return false;    }    return true;}int main(){    while(cin >> n){        for(int i = 0; i < n; i++){            int y;            cin >> y;            pii[i].first = i+1, pii[i].second = y;        }        double k = pii[1].second - pii[0].second;           //判断可不可只用一条直线连接所有点        bool flag = true;        for(int i = 1; i < n; i++){            if((pii[i].second - pii[0].second)/i != k)                {flag = false; break;}        }        if(flag){            printf("No\n"); continue;        }        k = pii[2].second - pii[1].second;            //判断第一个点单独放,剩下的点连直线        flag = true;        for(int i = 2; i < n; i++){            if((pii[i].second - pii[1].second)/(i-1) != k)      //除以的是i-1, 最后WA在这                {flag = false; break;}        }        if(flag){            printf("Yes\n"); continue;        }        flag = false;        map<double, int> mark;        memset(app, 0, sizeof(app));        for(int i = 1; i < n; i++){            app[0] = 1;            double k = (pii[i].second - pii[0].second) / i;            if(mark.count(k)) continue;            mark[k] = 1;            for(int j = 1; j < n; j++)                if((pii[j].second - pii[0].second) / j == k)   app[j] = 1;            if(judge(app, k)){                flag = true; break;            }            for(int j = 1; j < n; j++) app[j] = 0;        }        if(flag)            printf("Yes\n");        else            printf("No\n");    }    return 0;}

From Y to Y

/*time limit per test1 second
memory limit per test256 megabytes*/

From beginning till end, this message has been waiting to be conveyed.

For a given unordered multiset of n lowercase English letters (“multi” means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times:

Remove any two elements s and t from the set, and add their concatenation s + t to the set.
The cost of such operation is defined to be , where f(s, c) denotes the number of times character c appears in string s.

Given a non-negative integer k, construct any valid non-empty set of no more than 100 000 letters, such that the minimum accumulative cost of the whole process is exactly k. It can be shown that a solution always exists.

Input

The first and only line of input contains a non-negative integer k (0 ≤ k ≤ 100 000) — the required minimum cost.

Output

Output a non-empty string of no more than 100 000 lowercase English letters — any multiset satisfying the requirements, concatenated to be a string.

Note that the printed string doesn’t need to be the final concatenated string. It only needs to represent an unordered multiset of letters.

Examples

input
12
output
abababab
input
3
output
codeforces

Note

For the multiset {‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’, ‘a’, ‘b’}, one of the ways to complete the process is as follows:

{“ab”, “a”, “b”, “a”, “b”, “a”, “b”}, with a cost of 0;
{“aba”, “b”, “a”, “b”, “a”, “b”}, with a cost of 1;
{“abab”, “a”, “b”, “a”, “b”}, with a cost of 1;
{“abab”, “ab”, “a”, “b”}, with a cost of 0;
{“abab”, “aba”, “b”}, with a cost of 1;
{“abab”, “abab”}, with a cost of 1;
{“abababab”}, with a cost of 8.
The total cost is 12, and it can be proved to be the minimum cost of the process.

思路,多写几个规律就出来了

#include <bits/stdc++.h>using namespace std;typedef long long ll;ll a[20000];int main(){    int cnt = 0;    for(int i = 2; i*(i-1)/2 <= 2e5; i++, cnt++)    {        a[i-2] = i * (i-1) / 2;    }    int k;    while(cin >> k){        if(k == 0) {printf("a\n"); continue;}        int alp= 0;        while(k){            ll *it = upper_bound(a, a+cnt, k);            it --;            for(int i = 0; i < it-a+2; i++)                printf("%c", 'a'+alp);            k -= *it;            alp++;        }        printf("\n");    }    return 0;}

HDU 5860 Death sequence

Problem Description

You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over capture and decided that they would form a circle and start killing themselves using a step of three. Josephus states that by luck or maybe by the hand of God, he and another man remained the last and gave up to the Romans.

Now the problem is much easier: we have N men stand in a line and labeled from 1 to N, for each round, we choose the first man, the k+1-th one, the 2*k+1-th one and so on, until the end of the line. These poor guys will be kicked out of the line and we will execute them immediately (may be head chop, or just shoot them, whatever), and then we start the next round with the remaining guys. The little difference between the Romans and us is, in our version of story, NO ONE SURVIVES. Your goal is to find out the death sequence of the man.

For example, we have N = 7 prisoners, and we decided to kill every k=2 people in the line. At the beginning, the line looks like this:

1 2 3 4 5 6 7

after the first round, 1 3 5 7 will be executed, we have

2 4 6

and then, we will kill 2 6 in the second round. At last 4 will be executed. So, you need to output 1 3 5 7 2 6 4. Easy, right?

But the output maybe too large, we will give you Q queries, each one contains a number m, you need to tell me the m-th number in the death sequence.

Input

Multiple cases. The first line contains a number T, means the number of test case. For every case, there will be three integers N (1<=N<=3000000), K(1<=K), and Q(1<=Q<=1000000), which indicate the number of prisoners, the step length of killing, and the number of query. Next Q lines, each line contains one number m(1<=m<=n).

Output

For each query m, output the m-th number in the death sequence.

Sample Input

1
7 2 7
1
2
3
4
5
6
7

Sample Output

1
3
5
7
2
6
4

位置映射后,类似递归,找到规律

#include <bits/stdc++.h>using namespace std;pair<pair<int, int>, int> pii[3000010];int mark[3000010];int main(){    int n, k, q = 0;    int t;    cin >> t;    while(t--){        memset(mark,0,sizeof(mark));        cin >> n >> k >> q;        int j = 0;        for(int i = 0; i < n; i+=k){            pii[++j].first.first = 1;            pii[j].first.second = i/k+1;            pii[j].second = i;            mark[i] = j;        }        for(int i = 1; i < n; i++){            int t = i - i/k - 1;            if(mark[i]) continue;            pii[++j].second = i;            pii[j].first.first = pii[mark[t]].first.first + 1;            pii[j].first.second = pii[mark[t]].first.second;            mark[i] = j;        }        sort(pii+1, pii+n+1);        for(int i = 0; i < q; i++){            int m;            scanf("%d", &m);            printf("%d\n", pii[m].second+1);        }    }    return 0;}

屯一道题
cf 848B Rooter’s song

hint

Group dancers by p - t. What happens next?

model code

#include <cstdio>#include <algorithm>#include <vector>static const int MAXN = 100004;static const int MAXW = 100003;static const int MAXT = 100002;int n, w, h;int g[MAXN], p[MAXN], t[MAXN];std::vector<int> s[MAXW + MAXT];int ans_x[MAXN], ans_y[MAXN];int main(){    scanf("%d%d%d", &n, &w, &h);    for (int i = 0; i < n; ++i) {        scanf("%d%d%d", &g[i], &p[i], &t[i]);        s[p[i] - t[i] + MAXT].push_back(i);    }    std::vector<int> xs, ys;    for (int i = 0; i < MAXW + MAXT; ++i) if (!s[i].empty()) {        for (int u : s[i]) {            if (g[u] == 1) xs.push_back(p[u]);            else ys.push_back(p[u]);        }        std::sort(xs.begin(), xs.end());        std::sort(ys.begin(), ys.end());        std::sort(s[i].begin(), s[i].end(), [] (int u, int v) {            if (g[u] != g[v]) return (g[u] == 2);            else return (g[u] == 2 ? p[u] > p[v] : p[u] < p[v]);        });        for (int j = 0; j < xs.size(); ++j) {            ans_x[s[i][j]] = xs[j];            ans_y[s[i][j]] = h;        }        for (int j = 0; j < ys.size(); ++j) {            ans_x[s[i][j + xs.size()]] = w;            ans_y[s[i][j + xs.size()]] = ys[ys.size() - j - 1];        }        xs.clear();        ys.clear();    }    for (int i = 0; i < n; ++i) printf("%d %d\n", ans_x[i], ans_y[i]);    return 0;}