A
Maximum Multiple
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1857 Accepted Submission(s): 798
Problem Description
Given an integer n, Chiaki would like to find three positive integers x, y and z such that: n=x+y+z, x∣n, y∣n, z∣n and xyz is maximum.
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106).
Output
For each test case, output an integer denoting the maximum xyz. If there no such integers, output −1 instead.
Sample Input
3 1 2 3
Sample Output
-1 -1 1
打个表发现3的倍数和3的倍数存在规律。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
std::ios::sync_with_stdio(false);
int t;cin>>t;
while(t--)
{
ll n;cin>>n;
if(n>=3&&(n%3==0||n%4==0))
{
if(n%3==0) cout<<n/3*n/3*n/3<<endl;
else cout<<n/4*n/4*n/2<<endl;
}
else cout<<-1<<endl;
}
return 0;
}
B
Balanced Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4488 Accepted Submission(s): 1175
Problem Description
Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced:
+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.
Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) -- the number of strings.
Each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds 5×106.
Output
For each test case, output an integer denoting the answer.
Sample Input
2 1 )()(()( 2 ) )(
Sample Output
4 2
题意:给n个串,然后n个串可以任意排,问你最多匹配多少括号。
思路:没明显每个串自己跟自己匹配后,删除掉已经匹配的括号,存在四种情况,1空串,2只剩左括号(((,3只剩有括号))),4包含左右括号)))(((。可能有人会想到dp,但是这个题,很明显可以贪心,把')'少的的尽量放左边,'('少的,尽量放右边。这样就可以保证尽量多的括号被用到。
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <string.h>
#include <iostream>
using namespace std;
typedef long long ll;
struct node{
int l,r;
}a[100005];
int cmp(node a,node b)
{
if(a.l<=a.r&&b.l<=b.r) return a.l<b.l;
if(a.l<=a.r&&b.l>b.r) return 1;
if(a.l>a.r&&b.l<=b.r) return 0;
if(a.l>a.r&&b.l>b.r) return a.r>b.r;
}
char s[100006];
int maxn(int a,int b)
{
if(a>b) return a;
return b;
}
int main()
{
//std::ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
ll sum=0;
for(int i=0;i<n;i++)
{
int top=0,end=0;
scanf("%s",&s);
int len=strlen(s);
int ans=0,lans=0,rans=0;
for(int j=0;j<len;j++)
{
if(s[j]==')')
{
lans++;
if(top<end)
{
end--;
ans++;
sum++;
}
}
else
{
rans++;
end++;
}
}
a[i].l=lans-ans;a[i].r=rans-ans;
}
sort(a,a+n,cmp);
int l=0,r=0;
int num=0;
for(int i=0;i<n;i++)
{
if(a[i].l>num)
a[i].l=num;
sum+=a[i].l;
num-=a[i].l;
num+=a[i].r;
}
printf("%lld\n",sum*2);
}
return 0;
}
C
Triangle Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1289 Accepted Submission(s): 648
Special Judge
Problem Description
Chiaki has 3n points p1,p2,…,p3n. It is guaranteed that no three points are collinear.
Chiaki would like to construct n disjoint triangles where each vertex comes from the 3n points.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1000) -- the number of triangle to construct.
Each of the next 3n lines contains two integers xi and yi (−109≤xi,yi≤109).
It is guaranteed that the sum of all n does not exceed 10000.
Output
For each test case, output n lines contain three integers ai,bi,ci (1≤ai,bi,ci≤3n) each denoting the indices of points the i-th triangle use. If there are multiple solutions, you can output any of them.
Sample Input
1 1 1 2 2 3 3 5
Sample Output
1 2 3
思路:题目保证任意三点不共线,我们知道每个三角形内部肯定不能包含其他点,不然肯定会相交。那么我们按照x坐标排序,选择相邻三点就行了。
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <string.h>
#include <iostream>
using namespace std;
typedef long long ll;
struct node{
int x,y,pos;
}a[100050];
bool cmp(node q,node w)
{
if(q.x==w.x) return q.y<w.y;
return q.x<w.x;
}
int main()
{
//std::ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--)
{
int n;cin>>n;
for(int i=0;i<3*n;i++)
scanf("%d%d",&a[i].x,&a[i].y),a[i].pos=i+1;
sort(a,a+3*n,cmp);
for(int i=0;i<3*n;i+=3)
printf("%d %d %d\n",a[i].pos,a[i+1].pos,a[i+2].pos);
}
return 0;
}
Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3651 Accepted Submission(s): 1212
Problem Description
Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i<j≤r), ai≠ajholds.
Chiaki would like to find a lexicographically minimal array which meets the facts.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m (1≤n,m≤105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1≤li≤ri≤n).
It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
Output
For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.
Sample Input
3 2 1 1 2 4 2 1 2 3 4 5 2 1 3 2 4
Sample Output
1 2 1 2 1 2 1 2 3 1 1
set维护一下。然后用指针l,r维护区间,类似于莫队的方法。
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <string.h>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
int ans[1000006];
struct node
{
int l,r;
}q[1000006];
int cmp(node a, node b)
{
if (a.l == b.l) return a.r < b.r;
return a.l < b.l;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;scanf("%d %d",&n,&m);
int maxn=0;
for(int i=0; i<m; i++)
{
scanf("%d %d",&q[i].l,&q[i].r);
if(q[i].l>q[i].r) swap(q[i].l,q[i].r);
maxn=max(maxn,q[i].r-q[i].l+1);
}
sort(q,q+m,cmp);
set<int>st;
for (int i = 1 ; i <= n ; i++)
{
st.insert(i);
ans[i] = 1;
}
for (int i = q[0].l ; i <= q[0].r ; i++)
{
ans[i] = *st.begin();
st.erase(st.begin());
}
int l=q[0].l,r=q[0].r;
for(int i=0; i<m; i++)
{
while(l<q[i].l)
{
st.insert(ans[l]);
l++;
}
while(r<q[i].r)
{
if(r<q[i].l-1) r=q[i].l-1;
else
{
r++;ans[r]=*st.begin();
st.erase(st.begin());
}
}
}
for(int i=1; i<=n; i++) printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0;
}
Time Zone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2446 Accepted Submission(s): 768
Problem Description
Chiaki often participates in international competitive programming contests. The time zone becomes a big problem.
Given a time in Beijing time (UTC +8), Chiaki would like to know the time in another time zone s.
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains two integers a, b (0≤a≤23,0≤b≤59) and a string s in the format of "UTC+X'', "UTC-X'', "UTC+X.Y'', or "UTC-X.Y'' (0≤X,X.Y≤14,0≤Y≤9).
Output
For each test, output the time in the format of hh:mm (24-hour clock).
Sample Input
3 11 11 UTC+8 11 12 UTC+9 11 23 UTC+0
Sample Output
11:11 12:12 03:23
模拟时区,把时间都变成分钟就可以很方便的模拟了。
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[50];
int t;scanf("%d",&t);
while(t--)
{
int x,y;
scanf("%d %d %s",&x,&y,s);
int n=strlen(s);
char ch=s[3];
int f=0;
int temp=0,te=0;
for(int i=4;i<n;i++)
{
if(s[i]=='.') {
f=1;break;}
else temp=temp*10+(s[i]-'0');
}
if(f==1) te=s[n-1]-'0';
int tim=x*60+y;
//cout<<temp<<endl;
if(ch=='+'&&temp>=8)
{
tim+=(temp-8)*60+6*te;
}
else if(ch=='+'&&temp<8)
{
tim=tim-(8-temp)*60+te*6;
}
else
{
tim=tim-(temp+8)*60-6*te;
}
tim=(tim+24*60)%(24*60);
printf("%02d:%02d\n",tim/60,tim%60);
}
return 0;
}