題目鏈接:http://codeforces.com/contest/977
A:題意是對這個數進行k次操作,如果這個數能被10整除,就除以10,不能的話就減1。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long int
int main()
{
int n, k;
while (cin >> n >> k)
{
while (k--)
{
if (n % 10 == 0)
n /= 10;
else
n--;
}
cout << n << endl;
}
return 0;
}
B:題意是在給定的字符串中找出出現次數最多的連續兩個字符(我的寫法可能和正常解法不太一樣,正常解法好像都是用map然後嵌套一下,我是直接開了個二維數組暴力一下)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long int
int s[26][26];
int main()
{
int n;
char a[110];
while (cin>>n)
{
cin >> a;
memset(s, 0, sizeof(s));
for (int i = 0; i < n - 1; i++)
{
s[a[i] - 'A'][a[i + 1] - 'A']++;
}
int cmp = 0;
char res = 0;
char ans = 0;
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < 26; j++)
{
if (s[i][j]>cmp)
{
cmp = s[i][j];
res = 'A' + i;
ans = 'A' + j;
}
}
}
cout << res << ans << endl;
}
return 0;
}
C:輸出1-1e9之間的一個數,使得數組中有k個數小於等於這個數,如果沒有的話就輸出-1。(注意k的數據範圍是叢從0開始的,在這個地方wa出血了)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long int
int n,k;
int a[200200];
int main()
{
while (cin >> n >> k)
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
if (k == 0)
{
if (a[0] - 1 < 1)
cout << -1 << endl;
else
cout << a[0]-1 << endl;
continue;
}
int res = a[k - 1];
int cmp = upper_bound(a, a + n, res) - a;
if (cmp>k)
cout << -1 << endl;
else
cout << res << endl;
}
return 0;
}
D:(賽後清題)這道題目就是讓你排序,按照題意來講雖然排序之後大小是無序的,但是因爲操作是隻能除以3,乘以2,所以從前往後能對3進行除操作的個數必定遞減,而在這個基礎上如果操作次數相同則必定是按照能對2進行操作數進行升序輸出(按照值的大小升序輸出也可以,提交是ac的並且說起來更容易理解一點)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long int
struct node
{
LL num;
int three;
int two;
}s[110];
bool cmp(node a,node b)
{
if(a.three==b.three)
return a.two<b.two;
else
return a.three>b.three;
}
int main()
{
int n;
LL x;
while(cin>>n)
{
for(int i=0;i<n;i++)
{
scanf("%lld",&x);
s[i].num=x;
s[i].three=0;
s[i].two=0;
while(x%3==0)
{
s[i].three++;
x/=3;
}
while(x%2==0)
{
s[i].two++;
x/=2;
}
}
sort(s,s+n,cmp);
for(int i=0;i<n;i++)
printf("%lld ",s[i].num);
printf("\n");
}
return 0;
}
E:(賽後清題)題意是讓你求有多少個單圈環,我們不難發現單圈環中每個點的度都爲2,直接dfs判斷就行了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define LL long long int
int n, m;
int x, y;
bool flag = 0;
bool vis[200200];
vector<int>vec[200200];
void dfs(int now)
{
vis[now] = 1;
if (vec[now].size() != 2)
flag = 1;
for (int i = 0; i < vec[now].size(); i++)
{
if (vis[vec[now][i]])
continue;
dfs(vec[now][i]);
}
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 1; i <= n; i++)
vec[i].clear();
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
vec[x].push_back(y);
vec[y].push_back(x);
}
int res = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
{
if (vis[i])
continue;
flag = 0;
dfs(i);
if (!flag)
res++;
}
printf("%d\n",res);
}
return 0;
}
F:(賽後清題)這道題就是讓你求最長的連續遞增的子序列嘛,輸出長度和座標。用一個map記錄每個點此時的最大遞增長度,從中可以得出最長的長度以及得到最大長度的數是哪個。然後遍歷整個數組,按順序遇到就輸出就行了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
#define LL long long int
int n;
int a[200200];
map<int, int>mp;
int main()
{
while (~scanf("%d",&n))
{
int cmp = 0;
int res = 0;
mp.clear();
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
mp[a[i]] = mp[a[i] - 1] + 1;
if (mp[a[i]]>cmp)
{
cmp = mp[a[i]];
res = a[i];
}
}
res -= cmp - 1;
printf("%d\n", cmp);
int p = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == res)
{
if (p++)
printf(" ");
printf("%d", i+1);
res++;
}
if (p == cmp)
break;
}
printf("\n");
}
return 0;
}
CF第一次div3,只做出了3道題。其他人都是ak的,雖然當時感冒,但還是挺能說明水平的吧。