本文主要是介绍Codeforces Round #288 (Div. 2)(稍看即懂,不解释),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:http://codeforces.com/contest/508
//CF里可以直接int n; int a[n]。---奇葩1
//本该跳过而不是return 的,因为还没输入完(但可以A)---cf奇葩2
A:
#include <algorithm> #include <iostream> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <map> #include <set>using namespace std;#define rofeach(i,x) for(epyt(x)i=x.rbegin();i!=x.rend();i++) #define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++) #define dbgs(x) cerr << (#x) << " --> " << (x) << ' ' #define dbg(x) cerr << (#x) << " --> " << (x) << endl #define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++) #define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--) #define epyt(x) __typeof(x.rbegin()) #define type(x) __typeof(x.begin()) #define pii pair< int,int > #define mod 1000000007 #define ll long long #define pb push_back #define mp make_pair #define INF INT_MAX #define nd second #define st first #define endl '\n'const int N = 1e5+5; const int MAX = 1e9+5;int n,m,x,y,k,arr[1005][1005],asd;bool ctr(int x,int y){if(arr[x][y] && arr[x+1][y] && arr[x+1][y+1] && arr[x][y+1]) return 1;if(arr[x][y] && arr[x-1][y] && arr[x-1][y-1] && arr[x][y-1]) return 1;if(arr[x][y] && arr[x-1][y] && arr[x-1][y+1] && arr[x][y+1]) return 1;if(arr[x][y] && arr[x+1][y] && arr[x+1][y-1] && arr[x][y-1]) return 1;return 0; } int main(){scanf("%d %d %d",&n,&m,&k);FOR(i,1,k){ scanf("%d %d",&x,&y); //CF里可以直接int n; int a[n]。---奇葩1arr[x][y] = 1;if(ctr(x,y)){cout << i << endl;return 0; //本该跳过而不是return 的,因为还没输入完---cf奇葩2}}cout << 0 << endl;return 0; }
B:
题意:给一个奇数,交换两个不同位置的数字,求能得到的最大的偶数。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;char s[100010], ts[100010]; int n;bool gao(char *s) {int pre = -1;for (int i = 0; i < n - 1; i ++) {if ((s[i] - '0') % 2 == 0) { //左到右扫一遍,<则直接交换得答案,期间保留最后一位偶数下标if (s[i] < s[n - 1]) {swap(s[i], s[n - 1]);return true;}pre = i;}}if (pre != -1) {//若无<,则保留最后的一位偶数和末尾交换就是
swap(s[pre], s[n - 1]);return true;}return false; }int main() {scanf("%s", s);n = strlen(s);if ((s[n - 1] - '0') % 2 == 1) {strcpy(ts, s);bool sol = gao(ts);if (sol) {puts(ts);} else {puts("-1");}}return 0; }
C:
题意:
有m只鬼要来Anya的房间,Anya要保证在鬼到来的那一秒保证房间里至少有r只蜡烛在燃烧。(每只鬼来临时间不同)
点蜡烛是在一整秒时刻点(时间可以是负的),每秒只能点一只蜡烛。点亮后每只蜡烛会持续燃烧t秒后熄灭。求所需要的最少蜡烛数。
//☆思路还不错
#include<bits/stdc++.h>
using namespace std;
int ans,m,t,r,w[666],f[666]; //f[i];保持i秒时候亮的蜡烛数量
int main()
{cin>>m>>t>>r;for(int i=0;i<m;++i)cin>>w[i];sort(w,w+m);if(r>t) return puts("-1");for(int i=0;i<m;++i){for(int j=w[i]-1;f[w[i]]<r;j--)//w[i]的前一秒开始点蜡烛{for(int k=j+1;k<=t+j;++k)//蜡烛持续亮t秒if(k>=1)f[k]++; //t秒时亮的蜡烛数量+1ans++; //j秒点了根蜡烛}}cout<<ans;
}
D. Tanya and Password
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n?+?2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with n pieces of paper.
Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.
Input
The first line contains integer n (1?≤?n?≤?2·105), the number of three-letter substrings Tanya got.
Next n lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.
Output
If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO".
If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.
Sample test(s)
input
5
aca
aba
aba
cab
bac
output
YES
abacaba
input
4
abc
bCb
cb1
b13
output
NO
input
7
aaa
aaa
aaa
aaa
aaa
aaa
aaa
output
YES
aaaaaaaaa
如“abc”,拆成“ab”和“bc”,然后对ab和bc所属的编号加边。
#include <iostream>
#include <vector>
#include <algorithm>
#include<cstdio>
#include<string>
using namespace std;
vector<int> edges[66000];
int in[66000],out[66000],cnt[66000];
string ans;void dfs(int i)
{while(cnt[i]<edges[i].size())//cnt[i]:当edges[i].size()的下标{dfs(edges[i][cnt[i]++]);}ans+=(char)i%256; //% 取后者,ex:s[1]*256+s[2]后者是s[2]
}
int main()
{int n,u,v,start=0,i;scanf("%d",&n);string s;for( i=0;i<n;++i){cin>>s;u=s[0]*256+s[1];v=s[1]*256+s[2];edges[u].push_back(v);in[u]++; out[v]++;}start=u;int l=0,r=0;for(i=0;i<66000;++i){int d=in[i]-out[i];if(d==1){ l++; start=i; }else if(d==-1) r++;else if(d!=0){printf("NO\n");return 0;}}if(l>1 || r>1){printf("NO\n");return 0;}dfs(start);//cout<<char(u/256)<<" ans="<<ans<<endl;ans+=(char)(start/256);//cout<<" ans="<<ans<<endl;reverse(ans.begin(),ans.end());if(ans.length()!=n+2) printf("NO\n");else cout<<"YES\n"<<ans<<endl;return 0;
}
/*
需判if(ans.length()!=n+2)的样例
4
aba
bab
cdc
dcd
*/
E. Arthur and Brackets
time limit per test2 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard output
Notice that the memory limit is non-standard.
Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him.
All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the i-th opening bracket he remembers the segment [li,?ri], containing the distance to the corresponding closing bracket.
Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li,?ri].
Help Arthur restore his favorite correct bracket sequence!
Input
The first line contains integer n (1?≤?n?≤?600), the number of opening brackets in Arthur's favorite correct bracket sequence.
Next n lines contain numbers li and ri (1?≤?li?≤?ri?<?2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one.
The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.
Output
If it is possible to restore the correct bracket sequence by the given data, print any possible choice.
If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).
Sample test(s)
input
4
1 1
1 1
1 1
1 1
output
()()()()
input
3
5 5
3 3
1 1
output
((()))
input
3
5 5
3 3
2 2
output
IMPOSSIBLE
input
3
2 3
1 4
1 4
output
(())()
贪心版:(不明的话拿样例试试就非常清楚了)
#include <cstdio>
#include <stack>
using namespace std;
#define mm 700
int n, L[mm], R[mm],i;
char s[mm * 2];
int main()
{scanf("%d", &n);for(i = 0; i < n; ++i) scanf("%d%d", &L[i], &R[i]);int rear = 0; //当前字符串的末尾下标stack<pair<int, int> > st;for(i = 0; i < n; ++i){st.push(make_pair(L[i]+rear, R[i]+rear));s[rear++] = '(';while(!st.empty() && st.top().first <= rear && rear <= st.top().second){s[rear++] = ')';st.pop();}if(!st.empty() && rear > st.top().second) break;//超过栈顶区间范围,即配不上对了}if(!st.empty())puts("IMPOSSIBLE");else puts(s);return 0;
}
这篇关于Codeforces Round #288 (Div. 2)(稍看即懂,不解释)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!