C Mishka and the Last Exam

Whiteying

2018-12-16 19:30:54

Personal

# 题目链接: https://codeforces.com/contest/1093/problem/C # 原题: **Problem Statement** Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left. There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer ai to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it? Obviously Mishka didn't attend any classes. However, professor left some clues on the values of a to help out students like Mishka: - a was sorted in non-decreasing order (a1≤a2≤⋯≤an); - n was even; - the following sequence b, consisting of n2 elements, was formed and given out to students: bi=ai+an−i+1. Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable. Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b. **Input** The first line contains a single integer n (2≤n≤2⋅105) — the length of sequence a. n is always even. The second line contains n2 integers b1,b2,…,bn2 (0≤bi≤1018) — sequence b, where bi=ai+an−i+1. **It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.** **Output** Print n integers a1,a2,…,an (0≤ai≤1018) in a single line. a1≤a2≤⋯≤an should be satisfied. bi=ai+an−i+1 should be satisfied for all valid i. **Examples** **input** 4 5 6 **output** 2 3 3 3 **input** 6 2 1 2 **output** 0 0 1 1 1 2 **Note** In the first query we cannot rearrange letters to obtain a good string. Other examples (not all) of correct answers to the second query: "ababaca", "abcabaa", "baacaba". In the third query we can do nothing to obtain a good string. ------------ # 题意: 把一个回文变成一个非回文,若输入的本来就是非回文,就原样输出,若变不成非回文,就输出-1 # 思路: 三种情况: 1.回文,变不成非回文——>-1 2.回文,能变成非回文——>非回文 3.非回文——>原样输出 # AC代码: ``` #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; #include<algorithm> #include<queue> typedef long long ll; #include<vector> #define cin(n) scanf("%lld",&(n)) #define cout(n) printf("%lld",(n)) #define couc(c) printf("%c",(c)) #define coutn printf("\n") #define cout_ printf(" ") #define debug() printf("haha\n") const int MAXN= 1e6 + 5 ; ll t; ll n,k; ll a[1005]; ll ans; char s[1005]; int main() { cin(t); for(int i=1;i<=t;i++) { scanf("%s",s); int len=strlen(s); memset(a,0,sizeof(a)); int ans=0; int flag=0; for(int i=0;i<len/2;i++) { if(s[i]!=s[len-i-1]) { printf("%s\n",s); flag=1; break; } if(a[s[i]]==0) { ans++; } a[s[i]]++; if(a[s[len-i-1]]==0) { ans++; } a[s[len-i-1]]++; } if(len%2) { if(a[s[len/2]]==0) { ans++; } a[s[len/2]]++; } if(flag) continue; if(ans==1) printf("-1\n"); else { for(int i=1;i<1005;i++) while(a[i]) { printf("%c",i); a[i]--; } coutn; } } return 0; } ```